Jump to content

[TUT] How to make a Trivia system for chat


Unknown76

Recommended Posts

I this tutorial I will show you how to make Trivia system:

-- TRIVIA SCRIPT 
-- by QuantumZ - MTASA.com 
--------------------------- 
  
-- intializing trivia questions and answers 
  
local timeBetweenQuestions = 30000     -- first we need a variable for time between questions 
local reward = 1000           -- reward in dollars if answer is correct 
local timeToAnswer = 15000   -- time to answer for players before server will post the answer 
local numans  -- here we gonna generate the random number for a question 
  
local timeToAnswerTimer  -- we need this global timer 
  
-- all questions will be stored in the next variable 
local questions = { "Who was the last president of the Soviet Union?", 
                    "Where was Jesus arrested?", 
                    "What does C stand for in IRC?", 
                    "What group of Pacific islands did Japan attack the day after Pearl Harbor?", 
                    "From Earth, where was the destination of the longest long-distance telephone call?", 
                    "Which company launched CDs in the early 1990s?", 
                    "What does the first W stand for in WWW?", 
                    "What does Q mean in FAQ?", 
                    "What is the opposite of downloading?", 
                    "In 1998 a new breed of mosquito was discovered on which underground system?", 
                    "From what country vampires and werewolves originate?", 
                    "What's the largest Scandinavian country?", 
                    "What is the capital of Venezuela?", 
                    "How many countries are still members of the British Commonwealth?", 
                    "In which ocean are the Canary Islands?", 
                    "Which country produces 70% of the world's olive oil?", 
                    "What is the state Capital of Colorado?", 
                    "What does MTA stand for?", 
                    "What is the state capital of Massachusetts?", 
                    "What is the capital of Finland?", 
                    "The Vatican City is within which other city?", 
                    "On which island was the Mafia founded?", 
                    "In which country is Transylvania?", 
                    "Which is Europes most mountainous country?", 
                    "Alphabetically, what is the first country in the world?", 
                    "Which is the most common first name in the world?", 
                    "In which country is the Amazon rainforest?", 
                    "What's the Capital of Malaysia?", 
                    "What is the largest island in the Indian Ocean?", 
                    "Which of the continents has the most countries?", 
                    "Where is Pamplona?", 
                    "Who played Indiana Jones in the films?", 
                    "What actor starred in the 1999 film The Green Mile?", 
                    "Other than Marlon Brando which other actor has played the part of Vito Corleone in the Godfather series?" 
                  } 
  
local numberOfQuestions = #questions     -- we need to keep tracking the number of questions 
  
-- all answers will be stored in this variable (attention: question number and answer should be correspoding, be careful with the order)                 
local answers = { "Mikail Gorbachev", 
                  "Garden of Gethsemane", 
                  "Chat", 
                  "The Philippines", 
                  "Moon", 
                  "Philips", 
                  "World", 
                  "Question", 
                  "Uploading", 
                  "London", 
                  "Romania", 
                  "Sweden", 
                  "Caracus", 
                  "53", 
                  "Atlantic", 
                  "Greece", 
                  "Denver", 
                  "Multi Theft Auto", 
                  "Boston", 
                  "Helsinki", 
                  "Rome", 
                  "Sicily", 
                  "Romania", 
                  "Switzerland", 
                  "Afghanistan", 
                  "Mohammed", 
                  "Brazil", 
                  "Kuala Lumpu", 
                  "Madagascar", 
                  "Africa", 
                  "Spain", 
                  "Harrison Ford", 
                  "Tom Hanks", 
                  "Robert De Niro" 
                } 
  
-- lets start the question               
function initializeQuestion() 
        numans = math.random(numberOfQuestions)  -- generating a random numbers 
        outputChatBox("[TRIVIA QUESTION]", getRootElement(), 238, 218, 0, true)  -- marking the trivia start in chat 
        outputChatBox(questions[numans], getRootElement(), 219, 219, 212, true) -- output the chosen question 
        playSoundFrontEnd (getRootElement(), 5 )  -- play a fancy sound 
        timeToAnswerTimer = setTimer(stopQuestion, timeToAnswer, 1) -- start the timer which tracks the time allowed for players to answer 
end 
setTimer(initializeQuestion, 5000, 1)  -- this timer will be used only one time when resource starts  
  
-- when question stops  
function stopQuestion() 
        outputChatBox("#EE7600[TRIVIA] Nobody knows. Answer: #DBDB70"..answers[numans]..".", getRootElement(), 219, 219, 212, true) -- outputs in chatbox the answer for the question 
        numans = 0 -- we gonna reset the radom number 
        playSoundFrontEnd (getRootElement(), 19 ) -- play again a fancy sound 
        setTimer(initializeQuestion, timeBetweenQuestions, 1) -- lets initialize another question after some time (set in the variable timeBetweenQuestions above) 
end      
  
-- this function checks if an answer is correct 
function checkAnswers(messageclean, messageType) 
    if numans ~= 0 then -- if trivia is running (we dont want annoying messages in console if trivia is not running) 
        local message = string.lower(messageclean) -- lets lower all letters in the player's message 
        local answer = string.lower(answers[numans]) -- lets lower all letters in the answer 
        if message == answer then  -- if player's message corresponds with the  answer 
            givePlayerMoney ( source, reward ) -- lets give him some money 
            outputChatBox("#EE7600[TRIVIA] #DBDB70"..getPlayerName(source).."#EE7600 answers correctly, he is a genius. (+"..tostring(reward).."$)",  getRootElement(), 9, 249, 7, true) -- lets announce that he is a 'genius' in chat 
            setTimer(initializeQuestion, timeBetweenQuestions, 1) -- lets initialize another question after some time 
            playSoundFrontEnd (getRootElement(),  8 )  -- play a fancy sound ... again 
            numans = 0  -- reset the trivia random number (0 means trivia question is not running) 
            killTimer(timeToAnswerTimer) -- lets kill the timer timeToAnswerTimer 
        end 
    end      
end 
addEventHandler( "onPlayerChat", getRootElement(), checkAnswers ) -- when a player adds a message appeal the function checkAnswers 
  

EDIT: I uploaded this resource here: https://community.multitheftauto.com/index.php?p= ... ls&id=3432

If you have questions please post them below...

QuantumZ

Edited by Guest
  • Like 1
Link to comment
  • 2 weeks later...
I would of liked more kinda explaining in not LUA -tags. I meant that you should of explained everything in the thread, not in the script itself.

However you decided to do it your way and it was fine ;)

Next time I will make better explained tutorials. If you have any questions feel free to ask.

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...