• 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 [talkactions] attempt to call field 'broadcastMessage' (a nil value)

platano

Active Member
Joined
Jul 21, 2009
Messages
158
Solutions
1
Reaction score
37
Location
Mexico
Using: otx4 TFS 1.3

Hello everyone I'm trying to use a script

will run each hour, send questions every 60 seconds
answer questions with "!answer <answerhere>"

However I'm getting this error, hope to get some help :D

Bash:
[16/4/2020 13:7:30] [Error - GlobalEvent Interface]
[16/4/2020 13:7:30] data/globalevents/scripts/trivia.lua:onThink
[16/4/2020 13:7:30] Description:
[16/4/2020 13:7:30] data/globalevents/scripts/trivia.lua:5: attempt to call field 'broadcastMessage' (a nil value)
[16/4/2020 13:7:30] stack traceback:
[16/4/2020 13:7:30]     data/globalevents/scripts/trivia.lua:5: in function <data/globalevents/scripts/trivia.lua:4>
[16/4/2020 13:7:30] [Error - GlobalEvents::think] Couldn't execute event: trivia


Adding the script to test it
globalevents/globalevents.xml

Lua:
<globalevent name="trivia" interval="3600000" script="trivia.lua"/>

globalevents/scripts/trivia.lua
Lua:
local startTime = 3 -- Minutes until the event will start

function onThink(interval)
    Game.broadcastMessage("[TRIVIA] A trivia event will start in ".. startTime .." minutes.", MESSAGE_STATUS_CONSOLE_BLUE)
    addEvent(runTrivia, startTime * 60 * 1000)
    return true
end

talkactions/talkactions.xml
Lua:
<talkaction words="!answer" separator=" " script="trivia.lua"/>

talkactions/scripts/trivia.lua
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
 
Lua:
function broadcastMessage(message, messageType)
    Game.broadcastMessage(message, messageType)
    print("> Broadcasted message: \"" .. message .. "\".")
end

or just update your compat.lua in libs
 
Lua:
function broadcastMessage(message, messageType)
    Game.broadcastMessage(message, messageType)
    print("> Broadcasted message: \"" .. message .. "\".")
end

or just update your compat.lua in libs
data/globalevents/scripts/trivia.lua:5: attempt to call field 'broadcastMessage' (a nil value)
<table>.<field>(...)
 
what about broadcastMessage = doBroadcastMessage ?
That's completely irrelevant, which was my point.
He's not calling broadcastMessage, he's calling Game.broadcastMessage, where the broadcastMessage field of the Game table is missing, which is why I provided the link to the function.
 
@Infernum hey bro thanks for your reply

I'm using this distro

Should I just add:

to the directories /data/lib/core/game.lua#L1-L9 from previous reference or should I update something else in the project for a whole benefit?

I'm pretty new into tibia-servers coding but I get a lot of references right now because I'm into coding these quarantine days so please advice me if I should update something there for a whole benefit.

Btw I wanted to know if possible, how can I add 8.6 outfits + addons to this TFS? It's using a custom client but IDK where;s the source of the client lol
 
@Infernum hey bro thanks for your reply

I'm using this distro

Should I just add:

to the directories /data/lib/core/game.lua#L1-L9 from previous reference or should I update something else in the project for a whole benefit?

I'm pretty new into tibia-servers coding but I get a lot of references right now because I'm into coding these quarantine days so please advice me if I should update something there for a whole benefit.

Btw I wanted to know if possible, how can I add 8.6 outfits + addons to this TFS? It's using a custom client but IDK where;s the source of the client lol
Just add the function to the lib file.
 
Just add the function to the lib file.
Hey @Infernum sorry for the delay on replying last night we had terrible weather over my IP

EDIT:

Now this is happening, event triggers but it keeps saying it will start in 3 minutes it loops on that

Bash:
[17/4/2020 15:53:6] [Error - GlobalEvent Interface]
[17/4/2020 15:53:6] data/globalevents/scripts/trivia.lua:onThink
[17/4/2020 15:53:6] Description:
[17/4/2020 15:53:6] (LuaInterface::luaAddEvent) Callback parameter should be a function

Btw the source code I showed you up is like the fork of the server I'm using but it's not 100% that server, it's a fixed 7.6 idk but the whole lib directory is VERY different

1587145237292.png

Edit: I noticed the error was on the onThink so I searched for that in the IDE and I found that another function that well it's not bugged, uses this function:
doBroadcastMessage
instead of:
Game.broadcastMessage

Can anyone guess by experience what TFS I'm using lol I'm blind there's no reference in the code I downloaded


Added the whole script inside the globalevent.lua, it runs, but it throws this error after the 3 mins.

Bash:
[17/4/2020 16:3:49] [Error - GlobalEvent Interface]
[17/4/2020 16:3:49] In a timer event called from:
[17/4/2020 16:3:49] data/globalevents/scripts/trivia.lua:onThink
[17/4/2020 16:3:49] Description:
[17/4/2020 16:3:49] data/lib/lib.lua:7: attempt to index local 'player' (a number value)
[17/4/2020 16:3:49] stack traceback:
[17/4/2020 16:3:49]     data/lib/lib.lua:7: in function 'broadcastMessage'
[17/4/2020 16:3:49]     data/globalevents/scripts/trivia.lua:76: in function <data/globalevents/scripts/trivia.lua:67>
[17/4/2020 16:3:49] > Broadcasted message: "[TRIVIA] A trivia event will start in 3 minutes.".
 
Last edited:
Back
Top