Lua Lua Issue (math related)

Discussion in 'Support' started by Lava Titan, Mar 2, 2018.

  1. Lava Titan

    Lava Titan Developer

    Joined:
    Jul 25, 2009
    Messages:
    1,494
    Likes Received:
    65
    Best Answers:
    0
    So I got this very old TFS 0.4 MOD that works perfectely but I'm trying to make it work with math.random instead of text questions and answers..

    Example:

    Code (Lua):
    1. [1] = {question = "How much is "..number1.." + "..number2.."?", answer = ""..number1+number2..""}
    instead of


    Code (Lua):
    1. [1] = {question = "How much is 1 + 1?", answer = "2"}
    I made the tests in lua demo console and it works fine, I'm not sure what I missed so I ask for a extra pair of eyes to look at the script and tell me what am I doing wrong xd

    Here's the full script:

    Code (Lua):
    1.  
    2. <?xml version = "1.0" encoding = "UTF-8"?>
    3.    <mod name = "Pop quiz" version = "1.0" author = "Teckman" enabled = "yes">
    4.        <config name = "config"><![CDATA[
    5.            config = {
    6.            local number1 = math.random(1,10)
    7.            local number2 = math.random(1,10)
    8.                questions = {
    9.                    [1] = {question = "How much is "..number1.." + "..number2.."?", answer = ""..number1+number2..""}, -- this doesnt work
    10.                    [2] = {question = "What is the name of this game?", answer = "Tibia"}
    11.                }, -- this works
    12.                newItem = {2160, 1}, -- reward
    13.                storage = 400
    14.            }
    15.        ]]></config>
    16.        <globalevent name="popquiz" interval="60000" event="script"><![CDATA[
    17.            domodlib("config")
    18.            function onThink(interval)
    19.                setGlobalStorageValue(cid, config.storage, config.questions[math.random(1, table.maxn(config.questions))].question)
    20.                doBroadcastMessage("[QUIZ]: " .. getGlobalStorageValue(config.storage) .. " - to answer the question type '/quiz ANSWER'.", MESSAGE_EVENT_ADVANCE)
    21.                return true
    22.            end
    23.        ]]></globalevent>
    24.        <talkaction words = "/quiz" event = "script"><![CDATA[
    25.            domodlib("config")
    26.            function onSay(cid, words, param)
    27.                if(param) then
    28.                    if(getGlobalStorageValue(config.storage)) then
    29.                        for _, v in pairs(config.questions) do
    30.                            if(v.question == getGlobalStorageValue(config.storage)) then
    31.                                if(string.lower(param) == v.answer) then
    32.                                    doBroadcastMessage("[QUIZ]: " .. getPlayerName(cid) .. " has answered the question: " .. v.question .. ", with answer: " .. v.answer .. ". Congratulations to the winner!", MESSAGE_EVENT_ADVANCE)
    33.                                    doPlayerAddItem(cid, config.newItem[1], config.newItem[2])
    34.                                    setGlobalStorageValue(config.storage, nil)
    35.                                    doSendMagicEffect(getPlayerPosition(cid), 40)
    36.                                    return true
    37.                                end
    38.                            end
    39.                        end
    40.                    else
    41.                        doPlayerSendCancel(cid, "The quiz has already ended.")
    42.                        doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
    43.                        return true
    44.                    end
    45.                else
    46.                    doPlayerSendCancel(cid, "The answer must be stated in parameter.")
    47.                    doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
    48.                    return true
    49.                end
    50.                doPlayerSendCancel(cid, "This answer is wrong.")
    51.                doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
    52.                return true
    53.            end
    54.        ]]></talkaction>
    55.    </mod>
    56.  
     
  2. Best Answer:
    Post #8 by Ninja, Mar 16, 2018
  3. Itutorial

    Itutorial Well-Known Member

    Joined:
    Dec 23, 2014
    Messages:
    1,321
    Likes Received:
    274
    Best Answers:
    22
    So, a few problems there.

    1) If you put the math.random() outside of the code itself it will always be the same 2 numbers until the server restarts.
    2) You will have to make the config inside of the code as-well because you are trying to create the config with changing values.

    I don't mess with mod files but I will make a lua code you can use instead.

    GlobalEvent
    Code (Lua):
    1.  
    2. --Numbered questions are hard coded and will be random every time.--
    3. local globalStorage1 = {11111}
    4. local globalStorage2 = {11112}
    5.  
    6. local questions = {
    7.     [1] = {"What is the name of this game?", 1},
    8.     [2] = {"What company created this game?", 2}
    9. }
    10.  
    11. local msgAdd = " The first person to answer will win a reward!"
    12.  
    13. function onThink(cid, words, param)
    14.  
    15. if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0 then return false end
    16. if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then return false end
    17.  
    18. if math.random(1, 2) == 1 then
    19.     local QUESTION = questions[math.random(1, #questions)]
    20.    
    21.     if not QUESTION then return true end
    22.    
    23.     doBroadcastMessage(QUESTION[1]..""..msgAdd)
    24.     setGlobalStorageValue(globalStorage1, QUESTION[3])
    25. else
    26.     local num1 = math.random(1, 10)
    27.     local num2 = math.random(1, 10)
    28.     local answer = 0
    29.     local msg = ""
    30.    
    31.     if math.random(1, 2) == 1 then
    32.         answer = num1 + num2
    33.         msg = "What is the answer of: "..num1.. " + "..num2.."?"..msgAdd
    34.         doBroadcastMessage(msg)
    35.         setGlobalStorageValue(globalStorage2, answer)
    36.     else
    37.         answer = num1 * num2
    38.         msg = "What is the answer of: "..num1.. " * "..num2.."?"..msgAdd
    39.         doBroadcastMessage(msg)
    40.         setGlobalStorageValue(globalStorage2, answer)
    41.     end
    42. end
    43. return true
    44. end
    45.  
    Talkaction
    Code (Lua):
    1.  
    2.  
    3. local globalStorage1 = {11111}
    4. local globalStorage2 = {11112}
    5.  
    6. local answers = {
    7.     [1] = "Tibia",
    8.     [2] = "Cipsoft",
    9. }
    10.  
    11. function onSay(cid, words, param)
    12.     if (param == "") then
    13.         return doPlayerSendCancel(cid, "You must type an answer to the question.")
    14.     end
    15.  
    16.     if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) ~= -1 and getGlobalStorageValue(globalStorage2) >= 1 then
    17.         param = tonumber(param)
    18.      
    19.         if getGlobalStorageValue(globalStorage2) == param then
    20.             setGlobalStorageValue(globalStorage2, 0)
    21.             doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
    22.          
    23.             --Add rewards here--
    24.          
    25.         else
    26.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
    27.         end
    28.      
    29.     else
    30.         if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) ~= -1 and getGlobalStorageValue(globalStorage1) >= 1 then
    31.             local ANSWER = answers[getGlobalStorageValue(globalStorage1)]
    32.          
    33.             ANSWER = string.lower(ANSWER)
    34.             param = string.lower(param)
    35.          
    36.             if param == ANSWER then
    37.                 setGlobalStorageValue(globalStorage1, 0)
    38.                 doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
    39.          
    40.             --Add rewards here--
    41.          
    42.             else
    43.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
    44.             end
    45.         else
    46.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There is no question to answer right now.")
    47.         end
    48.     end
    49. return true
    50. end
    51.  
     
    Lava Titan likes this.
  4. Lava Titan

    Lava Titan Developer

    Joined:
    Jul 25, 2009
    Messages:
    1,494
    Likes Received:
    65
    Best Answers:
    0
    Thanks for your help! I tested and it's working fine except the text questions, when I answer I get the message that there is no quiz going on, I think it may be something related to the storages but not sure tho, I've been looking at the scripts carefully for the last 20 minutes and no improvement xD

    Btw, I changed this part of the code (I already had error before changing it) so if there's a quiz already going on, no matter if text or number there wont be another until last one answered xd

    Code (Lua):
    1. if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0 then return false end
    2. if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then return false end
    to:

    Code (Lua):
    1. if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0  and getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then print("there's already a quiz going on") return true end
     
  5. Itutorial

    Itutorial Well-Known Member

    Joined:
    Dec 23, 2014
    Messages:
    1,321
    Likes Received:
    274
    Best Answers:
    22
    Change global to this.

    Code (Lua):
    1.  
    2. function onThink(cid, words, param)
    3.  
    4. if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0  and getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then print("there's already a quiz going on") return true end
    5.  
    6. if math.random(1, 2) == 1 then
    7.     local QUESTION = questions[math.random(1, #questions)]
    8.  
    9.     if not QUESTION then return true end
    10.  
    11.     doBroadcastMessage(QUESTION[1]..""..msgAdd)
    12.     setGlobalStorageValue(globalStorage1, QUESTION[2])
    13. else
    14.     local num1 = math.random(1, 10)
    15.     local num2 = math.random(1, 10)
    16.     local answer = 0
    17.     local msg = ""
    18.  
    19.     if math.random(1, 2) == 1 then
    20.         answer = num1 + num2
    21.         msg = "What is the answer of: "..num1.. " + "..num2.."?"..msgAdd
    22.         doBroadcastMessage(msg)
    23.         setGlobalStorageValue(globalStorage2, answer)
    24.     else
    25.         answer = num1 * num2
    26.         msg = "What is the answer of: "..num1.. " * "..num2.."?"..msgAdd
    27.         doBroadcastMessage(msg)
    28.         setGlobalStorageValue(globalStorage2, answer)
    29.     end
    30. end
    31. return true
    32. end
    33.  
    Just needed to change a 3 to a 2
     
    Lava Titan likes this.
  6. Lava Titan

    Lava Titan Developer

    Joined:
    Jul 25, 2009
    Messages:
    1,494
    Likes Received:
    65
    Best Answers:
    0
    Sorry only had time to work on this now, thanks for your answer, I'm not sure what went wrong but something wrong is not right xD
    I legit don't know what went wrong, the code was working before, and suddenly start getting these console errors '-'

    Screenshot
    [​IMG]

    Code (Lua):
    1. <globalevent name="quiz" interval="30000" event="script" value="2018/quiz.lua"/>
    Code (Lua):
    1. --Numbered questions are hard coded and will be random every time.--
    2. local globalStorage1 = {12211}
    3. local globalStorage2 = {12212}
    4. local questions = {
    5.     [1] = {"What is the name of this game?", 1},
    6.     [2] = {"What company created this game?", 2}
    7. }
    8. local msgAdd = " The first person to answer will win a reward!"
    9. function onThink(interval, lastExecution)
    10. if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0 then return false end
    11. if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then return false end
    12. if math.random(1, 2) == 1 then
    13.     local QUESTION = questions[math.random(1, #questions)]
    14.  
    15.     if not QUESTION then return true end
    16.  
    17.     doBroadcastMessage(QUESTION[1]..""..msgAdd)
    18.     setGlobalStorageValue(globalStorage1, QUESTION[2])
    19. else
    20.     local num1 = math.random(1, 10)
    21.     local num2 = math.random(1, 10)
    22.     local answer = 0
    23.     local msg = ""
    24.  
    25.     if math.random(1, 2) == 1 then
    26.         answer = num1 + num2
    27.         msg = "What is the answer of: "..num1.. " + "..num2.."?"..msgAdd
    28.         doBroadcastMessage(msg)
    29.         setGlobalStorageValue(globalStorage2, answer)
    30.     else
    31.         answer = num1 * num2
    32.         msg = "What is the answer of: "..num1.. " * "..num2.."?"..msgAdd
    33.         doBroadcastMessage(msg)
    34.         setGlobalStorageValue(globalStorage2, answer)
    35.     end
    36. end
    37. return true
    38. end
    39.  
    Code (Lua):
    1. local globalStorage1 = {12211}
    2. local globalStorage2 = {12212}
    3. local answers = {
    4.     [1] = "Tibia",
    5.     [2] = "Cipsoft",
    6. }
    7. function onSay(cid, words, param)
    8.     if (param == "") then
    9.         return doPlayerSendCancel(cid, "You must type an answer to the question.")
    10.     end
    11.     if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) ~= -1 and getGlobalStorageValue(globalStorage2) >= 1 then
    12.         param = tonumber(param)
    13.  
    14.         if getGlobalStorageValue(globalStorage2) == param then
    15.             setGlobalStorageValue(globalStorage2, 0)
    16.             doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
    17.      
    18.             --Add rewards here--
    19.      
    20.         else
    21.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
    22.         end
    23.  
    24.     else
    25.         if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) ~= -1 and getGlobalStorageValue(globalStorage1) >= 1 then
    26.             local ANSWER = answers[getGlobalStorageValue(globalStorage1)]
    27.      
    28.             ANSWER = string.lower(ANSWER)
    29.             param = string.lower(param)
    30.      
    31.             if param == ANSWER then
    32.                 setGlobalStorageValue(globalStorage1, 0)
    33.                 doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
    34.      
    35.             --Add rewards here--
    36.      
    37.             else
    38.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
    39.             end
    40.         else
    41.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There is no question to answer right now.")
    42.         end
    43.     end
    44. return true
    45. end
    [​IMG]
     
  7. Lava Titan

    Lava Titan Developer

    Joined:
    Jul 25, 2009
    Messages:
    1,494
    Likes Received:
    65
    Best Answers:
    0
  8. Lava Titan

    Lava Titan Developer

    Joined:
    Jul 25, 2009
    Messages:
    1,494
    Likes Received:
    65
    Best Answers:
    0
    Bump xD
     
  9. Ninja

    Ninja Global Moderator Staff Member Global Moderator

    Joined:
    Apr 6, 2010
    Messages:
    5,925
    Likes Received:
    1,547
    Best Answers:
    26
    Best Answer
    It should be running now after altering a few lines: Revisions - Lua Issue (math related)

    P.S. I'm not going to rewrite the script for you this time, so you're on your own now! ;)
     
    Lava Titan likes this.
  10. Lava Titan

    Lava Titan Developer

    Joined:
    Jul 25, 2009
    Messages:
    1,494
    Likes Received:
    65
    Best Answers:
    0
    Thanks for the revision, it's ok you don't need to rewrite the script xD

    P.S: little question, is there a way to get item name by id in tfs 0.4? cuz so far all I found is by using uid but I have no idea how to get item uid using item id '-'
     

Share This Page

Loading...