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>