Kthxbye
New Member
- Joined
- Jul 11, 2012
- Messages
- 122
- Reaction score
- 2
Currently have to answer 3 questions to get a reward I want it to give a reward for every question answered correctly
Lua:
--// Config
--// All question answers must be in lowercase, if the answer is a number it must be a string (example: 6 -> "6")
local questions = {
{text = "What is 3+3?", answer = "6"},
{text = "What is 2+2?", answer = "4"},
{text = "What is 1+1?", answer = "2"}
}
--// Random reward for the winner
local rewards = {
{id = 2148, count = {1, 100}}
}
local broadcastType = MESSAGE_STATUS_CONSOLE_BLUE -- Message type used for broadcasting
local messageType = MESSAGE_STATUS_CONSOLE_ORANGE -- Message type used for sending player messages (not broadcasts)
local autoQuestion = 60 -- Amount of seconds that a question should automatically be sent
local nextQuestion = 60 -- Amount of seconds that a question will be sent after a player answers correctly
local maxAnswers = 3 -- Amount of answers needed to recieve a reward
--\\
--// Non-config
if awaitingEvent then
stopEvent(awaitingEvent)
end
playerAnswers = {}
questionCache = {}
currentQuestion = 0
--\\
function selectQuestion()
local index = math.random(#questions)
if (#questions == #questionCache) then
return endTrivia(false, " No questions left.")
end
while isInArray(questionCache, index) do
index = math.random(#questions)
end
questionCache[#questionCache+1] = index
currentQuestion = index
return questions[index]
end
function endTrivia(silent, reason)
currentQuestion = 0
questionCache = {}
playerAnswers = {}
stopEvent(awaitingEvent)
if not silent then
Game.broadcastMessage("[TRIVIA] Trivia event has been ended.".. reason or "", broadcastType)
end
end
--// Silently end current event on reload
endTrivia(true)
function runTrivia(forced)
if forced then
Game.broadcastMessage("[TRIVIA] No one won the previous round.", broadcastType)
end
local question = selectQuestion()
if not question then
return
end
awaitingEvent = addEvent(runTrivia, autoQuestion * 1000, true)
Game.broadcastMessage("[TRIVIA] ".. question.text, broadcastType)
end
function onSay(player, words, param)
local activeQuestion = questions[currentQuestion]
if (currentQuestion == 0) then
player:sendTextMessage(messageType, "There is no active question.")
return false
end
if (param == "") then
player:sendTextMessage(messageType, "Input an answer.")
return false
end
if (param:lower() ~= activeQuestion.answer) then
player:sendTextMessage(messageType, "Incorrect answer.")
return false
end
local guid = player:getGuid()
local name = player:getName()
playerAnswers[guid] = (playerAnswers[guid] or 0) + 1
if (playerAnswers[guid] == maxAnswers) then
local reward = rewards[math.random(#rewards)]
player:addItem(reward.id, math.random(reward.count[1], reward.count[2]))
player:sendTextMessage(messageType, "You have answered ".. maxAnswers .." questions correctly, you have won a reward.")
endTrivia(false, " ".. name .. " has won the event.")
else
player:sendTextMessage(messageType, ("You have answered the question correctly. You now have [%d / %d] points."):format(playerAnswers[guid], maxAnswers))
Game.broadcastMessage(('%s has answered the question correctly. The correct answer was "%s". Next question in %d seconds.'):format(name, activeQuestion.answer, nextQuestion), broadcastType)
currentQuestion = 0
stopEvent(awaitingEvent)
addEvent(runTrivia, nextQuestion * 1000)
end
return false
end