• 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!

Lua Lua Issue (math related)

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,529
Solutions
1
Reaction score
85
Location
Portugal
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:

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

instead of


Lua:
[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:

Lua:
<?xml version = "1.0" encoding = "UTF-8"?>
   <mod name = "Pop quiz" version = "1.0" author = "Teckman" enabled = "yes">
       <config name = "config"><![CDATA[
           config = {
           local number1 = math.random(1,10)
           local number2 = math.random(1,10)
               questions = {
                   [1] = {question = "How much is "..number1.." + "..number2.."?", answer = ""..number1+number2..""}, -- this doesnt work
                   [2] = {question = "What is the name of this game?", answer = "Tibia"}
               }, -- this works
               newItem = {2160, 1}, -- reward
               storage = 400
           }
       ]]></config>
       <globalevent name="popquiz" interval="60000" event="script"><![CDATA[
           domodlib("config")
           function onThink(interval)
               setGlobalStorageValue(cid, config.storage, config.questions[math.random(1, table.maxn(config.questions))].question)
               doBroadcastMessage("[QUIZ]: " .. getGlobalStorageValue(config.storage) .. " - to answer the question type '/quiz ANSWER'.", MESSAGE_EVENT_ADVANCE)
               return true
           end
       ]]></globalevent>
       <talkaction words = "/quiz" event = "script"><![CDATA[
           domodlib("config")
           function onSay(cid, words, param)
               if(param) then
                   if(getGlobalStorageValue(config.storage)) then
                       for _, v in pairs(config.questions) do
                           if(v.question == getGlobalStorageValue(config.storage)) then
                               if(string.lower(param) == v.answer) then
                                   doBroadcastMessage("[QUIZ]: " .. getPlayerName(cid) .. " has answered the question: " .. v.question .. ", with answer: " .. v.answer .. ". Congratulations to the winner!", MESSAGE_EVENT_ADVANCE)
                                   doPlayerAddItem(cid, config.newItem[1], config.newItem[2])
                                   setGlobalStorageValue(config.storage, nil)
                                   doSendMagicEffect(getPlayerPosition(cid), 40)
                                   return true
                               end
                           end
                       end
                   else
                       doPlayerSendCancel(cid, "The quiz has already ended.")
                       doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
                       return true
                   end
               else
                   doPlayerSendCancel(cid, "The answer must be stated in parameter.")
                   doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
                   return true
               end
               doPlayerSendCancel(cid, "This answer is wrong.")
               doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
               return true
           end
       ]]></talkaction>
   </mod>
 
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
Lua:
--Numbered questions are hard coded and will be random every time.--
local globalStorage1 = {11111}
local globalStorage2 = {11112}

local questions = {
    [1] = {"What is the name of this game?", 1},
    [2] = {"What company created this game?", 2}
}

local msgAdd = " The first person to answer will win a reward!"

function onThink(cid, words, param)

if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0 then return false end
if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then return false end

if math.random(1, 2) == 1 then
    local QUESTION = questions[math.random(1, #questions)]
   
    if not QUESTION then return true end
   
    doBroadcastMessage(QUESTION[1]..""..msgAdd)
    setGlobalStorageValue(globalStorage1, QUESTION[3])
else
    local num1 = math.random(1, 10)
    local num2 = math.random(1, 10)
    local answer = 0
    local msg = ""
   
    if math.random(1, 2) == 1 then
        answer = num1 + num2
        msg = "What is the answer of: "..num1.. " + "..num2.."?"..msgAdd
        doBroadcastMessage(msg)
        setGlobalStorageValue(globalStorage2, answer)
    else
        answer = num1 * num2
        msg = "What is the answer of: "..num1.. " * "..num2.."?"..msgAdd
        doBroadcastMessage(msg)
        setGlobalStorageValue(globalStorage2, answer)
    end
end
return true
end

Talkaction
Lua:
local globalStorage1 = {11111}
local globalStorage2 = {11112}

local answers = {
    [1] = "Tibia",
    [2] = "Cipsoft",
}

function onSay(cid, words, param)
    if (param == "") then
        return doPlayerSendCancel(cid, "You must type an answer to the question.")
    end
 
    if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) ~= -1 and getGlobalStorageValue(globalStorage2) >= 1 then
        param = tonumber(param)
     
        if getGlobalStorageValue(globalStorage2) == param then
            setGlobalStorageValue(globalStorage2, 0)
            doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
         
            --Add rewards here--
         
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
        end
     
    else
        if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) ~= -1 and getGlobalStorageValue(globalStorage1) >= 1 then
            local ANSWER = answers[getGlobalStorageValue(globalStorage1)]
         
            ANSWER = string.lower(ANSWER)
            param = string.lower(param)
         
            if param == ANSWER then
                setGlobalStorageValue(globalStorage1, 0)
                doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
         
            --Add rewards here--
         
            else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
            end
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There is no question to answer right now.")
        end
    end
return true
end
 
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

Lua:
if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0 then return false end
if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then return false end

to:

Lua:
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
 
Change global to this.

Lua:
function onThink(cid, words, param)

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

if math.random(1, 2) == 1 then
    local QUESTION = questions[math.random(1, #questions)]
  
    if not QUESTION then return true end
  
    doBroadcastMessage(QUESTION[1]..""..msgAdd)
    setGlobalStorageValue(globalStorage1, QUESTION[2])
else
    local num1 = math.random(1, 10)
    local num2 = math.random(1, 10)
    local answer = 0
    local msg = ""
  
    if math.random(1, 2) == 1 then
        answer = num1 + num2
        msg = "What is the answer of: "..num1.. " + "..num2.."?"..msgAdd
        doBroadcastMessage(msg)
        setGlobalStorageValue(globalStorage2, answer)
    else
        answer = num1 * num2
        msg = "What is the answer of: "..num1.. " * "..num2.."?"..msgAdd
        doBroadcastMessage(msg)
        setGlobalStorageValue(globalStorage2, answer)
    end
end
return true
end

Just needed to change a 3 to a 2
 
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
iqqqcn


Lua:
<globalevent name="quiz" interval="30000" event="script" value="2018/quiz.lua"/>

Lua:
--Numbered questions are hard coded and will be random every time.--
local globalStorage1 = {12211}
local globalStorage2 = {12212}
local questions = {
    [1] = {"What is the name of this game?", 1},
    [2] = {"What company created this game?", 2}
}
local msgAdd = " The first person to answer will win a reward!"
function onThink(interval, lastExecution)
if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) > 0 then return false end
if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) > 0 then return false end
if math.random(1, 2) == 1 then
    local QUESTION = questions[math.random(1, #questions)]
 
    if not QUESTION then return true end
 
    doBroadcastMessage(QUESTION[1]..""..msgAdd)
    setGlobalStorageValue(globalStorage1, QUESTION[2])
else
    local num1 = math.random(1, 10)
    local num2 = math.random(1, 10)
    local answer = 0
    local msg = ""
 
    if math.random(1, 2) == 1 then
        answer = num1 + num2
        msg = "What is the answer of: "..num1.. " + "..num2.."?"..msgAdd
        doBroadcastMessage(msg)
        setGlobalStorageValue(globalStorage2, answer)
    else
        answer = num1 * num2
        msg = "What is the answer of: "..num1.. " * "..num2.."?"..msgAdd
        doBroadcastMessage(msg)
        setGlobalStorageValue(globalStorage2, answer)
    end
end
return true
end

Lua:
local globalStorage1 = {12211}
local globalStorage2 = {12212}
local answers = {
    [1] = "Tibia",
    [2] = "Cipsoft",
}
function onSay(cid, words, param)
    if (param == "") then
        return doPlayerSendCancel(cid, "You must type an answer to the question.")
    end
    if getGlobalStorageValue(globalStorage2) ~= nil and getGlobalStorageValue(globalStorage2) ~= -1 and getGlobalStorageValue(globalStorage2) >= 1 then
        param = tonumber(param)
 
        if getGlobalStorageValue(globalStorage2) == param then
            setGlobalStorageValue(globalStorage2, 0)
            doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
     
            --Add rewards here--
     
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
        end
 
    else
        if getGlobalStorageValue(globalStorage1) ~= nil and getGlobalStorageValue(globalStorage1) ~= -1 and getGlobalStorageValue(globalStorage1) >= 1 then
            local ANSWER = answers[getGlobalStorageValue(globalStorage1)]
     
            ANSWER = string.lower(ANSWER)
            param = string.lower(param)
     
            if param == ANSWER then
                setGlobalStorageValue(globalStorage1, 0)
                doBroadcastMessage("Player: "..getPlayerName(cid).." was the first to answer correctly!.")
     
            --Add rewards here--
     
            else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry that is not the correct answer.")
            end
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There is no question to answer right now.")
        end
    end
return true
end
iqqpwd
 
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 '-'
 

Similar threads

Back
Top