• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 0.X TFS 0.3.6 Trivia mod not getting players answers

Gozzer

New Member
Joined
Jun 19, 2008
Messages
43
Reaction score
4
Hello I am having a problem I wanted to script a Trivia that happens every hour for players, so I made it as an XML mod script, and most of it functions, sends the message in broadcast, asks the question, tells the player to answer using !A !B !C or !D but then it never accepts the answers, it waits 10 seconds and then says "oops! answer incorrect" which is fine if the answer was indeed incorrect, but its not taking the answers any idea what I am missing here?

HTML:
<mod name="Trivia System" version="1.0" author="Gozzer" contact="Hidden" enabled="no">
    <config name="trivia_config"><![CDATA[
        config = {
            trivia_interval = "60 Minutes", -- Time interval for trivia event (only for broadcast message, real time you can set on globalevents.xml)
            rewards_id = {9971}, -- Rewards ID
            difficulty_rewards = {10, 20, 30, 40}, -- Rewards based on difficulty level (easy, medium, hard, veryhard)
            website = "no" -- Only if you have PHP scripts and database table for trivia data
        }
    ]]></config>
    <globalevent name="trivia" interval="3600" event="script"><![CDATA[
        domodlib('trivia_config')
        function onThink(interval, lastExecution)
            local questions = {
                easy = {
                    { question = "What is the capital of France?", choices = { "London", "Berlin", "Paris", "Madrid" }, correct = 3 },
                    -- Add more questions and answers
                },
                medium = {
                    { question = "What is the largest planet in our solar system?", choices = { "Earth", "Mars", "Saturn", "Jupiter" }, correct = 4 },
                    -- Add more questions and answers
                },
                hard = {
                    { question = "Who painted the Mona Lisa?", choices = { "Michelangelo", "Leonardo da Vinci", "Pablo Picasso", "Vincent van Gogh" }, correct = 2 },
                    -- Add more questions and answers
                },
                veryhard = {
                    { question = "In what year was the first photograph taken?", choices = { "1826", "1892", "1765", "1950" }, correct = 1 },
                    -- Add more questions and answers
                }
            }
        
            local difficulty = math.random(1, 4)  -- Randomly choose difficulty: 1 to 4
            local chosenDifficulty = ""
            if difficulty == 1 then
                chosenDifficulty = "easy"
            elseif difficulty == 2 then
                chosenDifficulty = "medium"
            elseif difficulty == 3 then
                chosenDifficulty = "hard"
            else
                chosenDifficulty = "veryhard"
            end
            
            local randomQuestion = questions[chosenDifficulty][math.random(#questions[chosenDifficulty])]
            
            -- Broadcast the question to all players
            doBroadcastMessage("Trivia Question: " .. randomQuestion.question .. " A) " .. randomQuestion.choices[1] ..
                               " B) " .. randomQuestion.choices[2] .. " C) " .. randomQuestion.choices[3] ..
                               " D) " .. randomQuestion.choices[4] .. " (Type !A, !B, !C, or !D to answer)")
            
            -- Set up a listener for player answers using talkaction commands
            local answerListeners = {}
            for _, player in ipairs(getPlayersOnline()) do
                answerListeners[player] = addEvent(function()
                    local answer = getPlayerStorageValue(player, "trivia_response")
                    if answer then
                        local correctChoice = string.char(64 + randomQuestion.correct)  -- Convert correct answer to A-D
                        if string.upper(answer) == correctChoice then
                            local reward = config.difficulty_rewards[difficulty] -- Reward based on difficulty level
                            doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Congratulations! You answered correctly and won " .. reward .. " gold Bars.")
                            doPlayerAddItem(player, config.rewards_id[math.random(1, #config.rewards_id)], reward)  -- Add gold to player's inventory
                            doRemoveCreature(player)  -- Remove the talkaction listener
                            doBroadcastMessage(getPlayerName(player) .. " answered correctly and won " .. reward .. " gold bars!")
                        else
                            doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Oops! Your answer is incorrect.")
                        end
                        doRemoveEvent(answerListeners[player])
                        setPlayerStorageValue(player, "trivia_response", nil)
                    end
                end, 10000) -- Allow 10 seconds for answer
                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Type !A, !B, !C, or !D to answer the trivia question.")
            end
            
            return true
        end
    ]]></globalevent>
</mod>
 
XML:
<mod name="Trivia System" version="1.0" author="Gozzer" contact="Hidden" enabled="no">
    <config name="trivia_config"><![CDATA[
        config = {
            trivia_interval = "60 Minutes", -- Time interval for trivia event (only for broadcast message, real time you can set on globalevents.xml)
            rewards_id = {9971}, -- Rewards ID
            difficulty_rewards = {10, 20, 30, 40}, -- Rewards based on difficulty level (easy, medium, hard, veryhard)
            website = "no" -- Only if you have PHP scripts and database table for trivia data
        }
    ]]></config>
    <globalevent name="trivia" interval="3600" event="script"><![CDATA[
        domodlib('trivia_config')
        function onThink(interval, lastExecution)
            local questions = {
                easy = {
                    { question = "What is the capital of France?", choices = { "London", "Berlin", "Paris", "Madrid" }, correct = 3 },
                    -- Add more questions and answers
                },
                medium = {
                    { question = "What is the largest planet in our solar system?", choices = { "Earth", "Mars", "Saturn", "Jupiter" }, correct = 4 },
                    -- Add more questions and answers
                },
                hard = {
                    { question = "Who painted the Mona Lisa?", choices = { "Michelangelo", "Leonardo da Vinci", "Pablo Picasso", "Vincent van Gogh" }, correct = 2 },
                    -- Add more questions and answers
                },
                veryhard = {
                    { question = "In what year was the first photograph taken?", choices = { "1826", "1892", "1765", "1950" }, correct = 1 },
                    -- Add more questions and answers
                }
            }
        
            local difficulty = math.random(1, 4)  -- Randomly choose difficulty: 1 to 4
            local chosenDifficulty = ""
            if difficulty == 1 then
                chosenDifficulty = "easy"
            elseif difficulty == 2 then
                chosenDifficulty = "medium"
            elseif difficulty == 3 then
                chosenDifficulty = "hard"
            else
                chosenDifficulty = "veryhard"
            end
            
            local randomQuestion = questions[chosenDifficulty][math.random(#questions[chosenDifficulty])]
            
            -- Broadcast the question to all players
            doBroadcastMessage("Trivia Question: " .. randomQuestion.question .. " A) " .. randomQuestion.choices[1] ..
                               " B) " .. randomQuestion.choices[2] .. " C) " .. randomQuestion.choices[3] ..
                               " D) " .. randomQuestion.choices[4] .. " (Type !A, !B, !C, or !D to answer)")
            
            -- Set up a listener for player answers using talkaction commands
-- Set up a listener for player answers using talkaction commands
local answerListeners = {}
for _, player in ipairs(getPlayersOnline()) do
    doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Type !A, !B, !C, or !D to answer the trivia question.")
end

addEvent(function()
    for _, player in ipairs(getPlayersOnline()) do
        local answer = getPlayerStorageValue(player, "trivia_response")
        if answer then
            local correctChoice = string.char(64 + randomQuestion.correct)  -- Convert correct answer to A-D
            if string.upper(answer) == correctChoice then
                local reward = config.difficulty_rewards[difficulty] -- Reward based on difficulty level
                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Congratulations! You answered correctly and won " .. reward .. " gold Bars.")
                doPlayerAddItem(player, config.rewards_id[math.random(1, #config.rewards_id)], reward)  -- Add gold to player's inventory
                doBroadcastMessage(getPlayerName(player) .. " answered correctly and won " .. reward .. " gold bars!")
            else
                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Oops! Your answer is incorrect.")
            end
            setPlayerStorageValue(player, "trivia_response", nil)
        end
    end
end, 10000) -- Allow 10 seconds for answer

                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Type !A, !B, !C, or !D to answer the trivia question.")
            end
            
            return true
        end
    ]]></globalevent>
</mod>
 
You did not include the talkaction?

Where is the storage "trivia_response" saved?
That is a good question, I am still learning the whole scripting thing, probably why I can't get it to work lol xD well I will try again, unless someone else has input on what direction I should go from here xD
Post automatically merged:

XML:
<mod name="Trivia System" version="1.0" author="Gozzer" contact="Hidden" enabled="no">
    <config name="trivia_config"><![CDATA[
        config = {
            trivia_interval = "60 Minutes", -- Time interval for trivia event (only for broadcast message, real time you can set on globalevents.xml)
            rewards_id = {9971}, -- Rewards ID
            difficulty_rewards = {10, 20, 30, 40}, -- Rewards based on difficulty level (easy, medium, hard, veryhard)
            website = "no" -- Only if you have PHP scripts and database table for trivia data
        }
    ]]></config>
    <globalevent name="trivia" interval="3600" event="script"><![CDATA[
        domodlib('trivia_config')
        function onThink(interval, lastExecution)
            local questions = {
                easy = {
                    { question = "What is the capital of France?", choices = { "London", "Berlin", "Paris", "Madrid" }, correct = 3 },
                    -- Add more questions and answers
                },
                medium = {
                    { question = "What is the largest planet in our solar system?", choices = { "Earth", "Mars", "Saturn", "Jupiter" }, correct = 4 },
                    -- Add more questions and answers
                },
                hard = {
                    { question = "Who painted the Mona Lisa?", choices = { "Michelangelo", "Leonardo da Vinci", "Pablo Picasso", "Vincent van Gogh" }, correct = 2 },
                    -- Add more questions and answers
                },
                veryhard = {
                    { question = "In what year was the first photograph taken?", choices = { "1826", "1892", "1765", "1950" }, correct = 1 },
                    -- Add more questions and answers
                }
            }
      
            local difficulty = math.random(1, 4)  -- Randomly choose difficulty: 1 to 4
            local chosenDifficulty = ""
            if difficulty == 1 then
                chosenDifficulty = "easy"
            elseif difficulty == 2 then
                chosenDifficulty = "medium"
            elseif difficulty == 3 then
                chosenDifficulty = "hard"
            else
                chosenDifficulty = "veryhard"
            end
          
            local randomQuestion = questions[chosenDifficulty][math.random(#questions[chosenDifficulty])]
          
            -- Broadcast the question to all players
            doBroadcastMessage("Trivia Question: " .. randomQuestion.question .. " A) " .. randomQuestion.choices[1] ..
                               " B) " .. randomQuestion.choices[2] .. " C) " .. randomQuestion.choices[3] ..
                               " D) " .. randomQuestion.choices[4] .. " (Type !A, !B, !C, or !D to answer)")
          
            -- Set up a listener for player answers using talkaction commands
-- Set up a listener for player answers using talkaction commands
local answerListeners = {}
for _, player in ipairs(getPlayersOnline()) do
    doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Type !A, !B, !C, or !D to answer the trivia question.")
end

addEvent(function()
    for _, player in ipairs(getPlayersOnline()) do
        local answer = getPlayerStorageValue(player, "trivia_response")
        if answer then
            local correctChoice = string.char(64 + randomQuestion.correct)  -- Convert correct answer to A-D
            if string.upper(answer) == correctChoice then
                local reward = config.difficulty_rewards[difficulty] -- Reward based on difficulty level
                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Congratulations! You answered correctly and won " .. reward .. " gold Bars.")
                doPlayerAddItem(player, config.rewards_id[math.random(1, #config.rewards_id)], reward)  -- Add gold to player's inventory
                doBroadcastMessage(getPlayerName(player) .. " answered correctly and won " .. reward .. " gold bars!")
            else
                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Oops! Your answer is incorrect.")
            end
            setPlayerStorageValue(player, "trivia_response", nil)
        end
    end
end, 10000) -- Allow 10 seconds for answer

                doPlayerSendTextMessage(player, MESSAGE_EVENT_ADVANCE, "Type !A, !B, !C, or !D to answer the trivia question.")
            end
          
            return true
        end
    ]]></globalevent>
</mod>
I tried this and it instantly crashes on launch :(
HTML:
> Loading Trivia.xml...Assertion failed: m_scriptenvindex >= 0 && m_scriptenvindex < 21, file ../lusascript.h line 251
 
That is a good question, I am still learning the whole scripting thing, probably why I can't get it to work lol xD well I will try again, unless someone else has input on what direction I should go from here xD
Post automatically merged:


I tried this and it instantly crashes on launch :(
HTML:
> Loading Trivia.xml...Assertion failed: m_scriptenvindex >= 0 && m_scriptenvindex < 21, file ../lusascript.h line 251

If you wrote that script yourself, then you already know something about scripting ;)

Well, your whole concept is missing a command registration !A !B etc.

Because here, you check for storage value:
Lua:
local answer = getPlayerStorageValue(player, "trivia_response")
if answer then

But you need a talkaction (command) that will set this "storage value".

Something like this: (untested) - you can add it to your mod.xml
Lua:
<talkaction words="!A; !B; !C; !D" event="buffer"><![CDATA[
        local answer = words:sub(2) -- get the second character of the talkaction player used (A, B, C or D)
        setPlayerStorageValue(cid, "trivia_response", answer) -- set it as storage
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Thanks for voting.")
    ]]></talkaction>
 
If you wrote that script yourself, then you already know something about scripting ;)

Well, your whole concept is missing a command registration !A !B etc.

Because here, you check for storage value:
Lua:
local answer = getPlayerStorageValue(player, "trivia_response")
if answer then

But you need a talkaction (command) that will set this "storage value".

Something like this: (untested) - you can add it to your mod.xml
Lua:
<talkaction words="!A; !B; !C; !D" event="buffer"><![CDATA[
        local answer = words:sub(2) -- get the second character of the talkaction player used (A, B, C or D)
        setPlayerStorageValue(cid, "trivia_response", answer) -- set it as storage
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Thanks for voting.")
    ]]></talkaction>
Thanks I added that in, and it says "thanks for voting" but it doesn't give rewards then, I am going to tweak it a bit, I appreciate your help :D I am self-teaching as much as I can, I just learned how to do custom sprites aswell xD making for an interesting server I am working on :D
 
Back
Top