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

Solved afk script talkaction

Na Amigo

The crazy girl
Joined
Jun 5, 2017
Messages
254
Solutions
3
Reaction score
18
Location
Egypt
i've this script and it's not working and no error at my console
Code:
<talkaction words="!afk" event="script" script="afk.lua"/>
Code:
local function doSendAutoMessage(cid, pos)
local FRASES = {"Away!", "AFK!", "No here!"}
local TEMPO = 5
local npos = getThingPos(cid)

if (isCreature(cid) == TRUE) then
if (pos.x == npos.x) and (pos.y == npos.y) and (pos.z == npos.z) then
doSendAnimatedText(pos, FRASES[math.random(#FRASES)], 5914)
doSendMagicEffect(pos, 59)
addEvent(doSendAutoMessage, TEMPO*1000, cid, npos)
end
end
end

function onSay(cid, words, param)
pos = getThingPos(cid)
doSendAnimatedText(pos, FRASES[math.random(#FRASES)], 5914)
doSendMagicEffect(pos, 59)
addEvent(doSendAutoMessage, TEMPO*1000, cid, pos)
doPlayerSendCancel(cid, "You are in 'Afk' mode.")
return TRUE
end
 
Solution
the problem from this i'v past this it worked then Thank you for your efforts. I am very sorry
Code:
<talkaction words="!afk;/commands" event="script" value="afk.lua"/>
Try this:
Lua:
local c = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}

local function sendText(cid, text, pos)
    if isPlayer(cid) then
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        return setGlobalStorageValue(c.storage, addEvent(sendText, c.interval * 1000, cid, text, pos))
    end
    return stopEvent(getGlobalStorageValue(c.storage))
end

function onSay(cid, words, param)
    local v = getGlobalStorageValue(c.storage)
    if v ~= -1 then
        doPlayerSendCancel(cid, "You are now in 'Active' mode.")
        setGlobalStorageValue(c.storage, -1)
        return stopEvent(v)
    end
 
    doPlayerSendCancel(cid, "You are now in 'Afk' mode.")
    return sendText(cid, c.messages, getThingPos(cid))
end
 
Try this:
Lua:
local c = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}

local function sendText(cid, text, pos)
    if isPlayer(cid) then
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        return setGlobalStorageValue(c.storage, addEvent(sendText, c.interval * 1000, cid, text, pos))
    end
    return stopEvent(getGlobalStorageValue(c.storage))
end

function onSay(cid, words, param)
    local v = getGlobalStorageValue(c.storage)
    if v ~= -1 then
        doPlayerSendCancel(cid, "You are now in 'Active' mode.")
        setGlobalStorageValue(c.storage, -1)
        return stopEvent(v)
    end
 
    doPlayerSendCancel(cid, "You are now in 'Afk' mode.")
    return sendText(cid, c.messages, getThingPos(cid))
end
still not working i'v tfs 0.3.7_svn
 
I've got this script. Perhaps it works for you.
!afk on / !afk off

Lua:
local time = 5
     local say_events = {}
local function SayText(cid)
    if isPlayer(cid) == TRUE then
         if say_events[getPlayerGUID(cid)] ~= nil then
             if isPlayer(cid) == TRUE then
                 doSendAnimatedText(getPlayerPosition(cid),"AFK", math.random(01,255))
             end
             say_events[getPlayerGUID(cid)] = addEvent(SayText, time * 1000 / 2, cid)       
         end                                                       
    end
    return TRUE
end
function onSay(cid, words, param, channel)
    if(param == '') then
      doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
     return true
     end
     if param == "on" then
        if isPlayer(cid) == TRUE then
            doSendAnimatedText(getPlayerPosition(cid),"AFK", math.random(01,255))
        end
        say_events[getPlayerGUID(cid)] = addEvent(SayText, time * 1000, cid)
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_WARNING,"AFK mode enabled.")
     elseif param == "off" then
            stopEvent(say_events[getPlayerGUID(cid)])
            say_events[getPlayerGUID(cid)] = nil
            doPlayerSendTextMessage(cid,MESSAGE_STATUS_WARNING,"AFK mode disabled.")
    end
    return TRUE
end

XML:
<talkaction access="0-4" words="!afk" event="script" value="afk.lua"/>
 
I've got this script. Perhaps it works for you.
!afk on / !afk off

Lua:
local time = 5
     local say_events = {}
local function SayText(cid)
    if isPlayer(cid) == TRUE then
         if say_events[getPlayerGUID(cid)] ~= nil then
             if isPlayer(cid) == TRUE then
                 doSendAnimatedText(getPlayerPosition(cid),"AFK", math.random(01,255))
             end
             say_events[getPlayerGUID(cid)] = addEvent(SayText, time * 1000 / 2, cid)      
         end                                                      
    end
    return TRUE
end
function onSay(cid, words, param, channel)
    if(param == '') then
      doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
     return true
     end
     if param == "on" then
        if isPlayer(cid) == TRUE then
            doSendAnimatedText(getPlayerPosition(cid),"AFK", math.random(01,255))
        end
        say_events[getPlayerGUID(cid)] = addEvent(SayText, time * 1000, cid)
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_WARNING,"AFK mode enabled.")
     elseif param == "off" then
            stopEvent(say_events[getPlayerGUID(cid)])
            say_events[getPlayerGUID(cid)] = nil
            doPlayerSendTextMessage(cid,MESSAGE_STATUS_WARNING,"AFK mode disabled.")
    end
    return TRUE
end

XML:
<talkaction access="0-4" words="!afk" event="script" value="afk.lua"/>
this also not working
 
Okay, let me check my crystal ball and get back to you.

Or maybe you can tell us what your console says when executing the script?
 
Nothing? So add this instead and tell me what numbers you see in your console:

Lua:
local c = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}
local function sendText(cid, text, pos)
    print("4")
    if isPlayer(cid) then
        print("5")
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        return setGlobalStorageValue(c.storage, addEvent(sendText, c.interval * 1000, cid, text, pos))
    end
    print("6")
    return stopEvent(getGlobalStorageValue(c.storage))
end
function onSay(cid, words, param)
    local v = getGlobalStorageValue(c.storage)
    print("1")
    if v ~= -1 then
        print("2")
        doPlayerSendCancel(cid, "You are now in 'Active' mode.")
        setGlobalStorageValue(c.storage, -1)
        return stopEvent(v)
    end
    doPlayerSendCancel(cid, "You are now in 'Afk' mode.")
    print("3")
    return sendText(cid, c.messages, getThingPos(cid))
end
 
Nothing? So add this instead and tell me what numbers you see in your console:

Lua:
local c = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}
local function sendText(cid, text, pos)
    print("4")
    if isPlayer(cid) then
        print("5")
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        return setGlobalStorageValue(c.storage, addEvent(sendText, c.interval * 1000, cid, text, pos))
    end
    print("6")
    return stopEvent(getGlobalStorageValue(c.storage))
end
function onSay(cid, words, param)
    local v = getGlobalStorageValue(c.storage)
    print("1")
    if v ~= -1 then
        print("2")
        doPlayerSendCancel(cid, "You are now in 'Active' mode.")
        setGlobalStorageValue(c.storage, -1)
        return stopEvent(v)
    end
    doPlayerSendCancel(cid, "You are now in 'Afk' mode.")
    print("3")
    return sendText(cid, c.messages, getThingPos(cid))
end
Code:
<talkaction words="!afk" event="script" script="afk.lua"/>
nothing happen when i use !afk and nothing appear at console
 
Nothing? So add this instead and tell me what numbers you see in your console:

Lua:
local c = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}
local function sendText(cid, text, pos)
    print("4")
    if isPlayer(cid) then
        print("5")
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        return setGlobalStorageValue(c.storage, addEvent(sendText, c.interval * 1000, cid, text, pos))
    end
    print("6")
    return stopEvent(getGlobalStorageValue(c.storage))
end
function onSay(cid, words, param)
    local v = getGlobalStorageValue(c.storage)
    print("1")
    if v ~= -1 then
        print("2")
        doPlayerSendCancel(cid, "You are now in 'Active' mode.")
        setGlobalStorageValue(c.storage, -1)
        return stopEvent(v)
    end
    doPlayerSendCancel(cid, "You are now in 'Afk' mode.")
    print("3")
    return sendText(cid, c.messages, getThingPos(cid))
end
I understand what the script is trying to accomplish.. but it just seems all wrong.

globalstorage works for everyone on the world, not just an individual player.
even if the script was fully functioning, the first player to use the command would turn it on, and the second player would turn it off.

stopEvent won't work the way your wanting like that.., and again even if you did stop the event '1000', it'd stop it for everyone.

ugh, I'll rewrite it.
Lua:
local c = {
    interval = 5,
    messages = {"Away!", "AFK!", "Not here!"},
    afk_players = {}
}

local function sendAfkText(cid)
    if isPlayer(cid) and c.afk_players[cid] = 1 then
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        addEvent(sendAfkText, c.interval * 1000, cid)
    else
        c.afk_players[cid] = 0
    end
end

function onSay(cid, words, param)
    if not c.afk_players[cid] then
        c.afk_players[cid] = 0
    end
    if c.afk_players[cid] == 1 then
        doPlayerSendCancel(cid, "Afk mode disabled.")
        c.afk_players[cid] = 0
        return true
    end
    c.afk_players[cid] = 1
    doPlayerSendCancel(cid, "Afk mode enabled.")
    addEvent(sendAfkText, 0, cid)
    return true
end
 
This one has an inherent problem, if someone were to turn it on and off multiple times inbetween the interval time it would send multiple afk messages.
It'll need an exhaust of some sort to disable that.

The only other way to do it is to make each event unique and stop it with stopEvent.. but I can't think of another way to make the event unique to each person who uses it though.
Maybe someone has a better idea then using cid?
 
I understand what the script is trying to accomplish.. but it just seems all wrong.

globalstorage works for everyone on the world, not just an individual player.
even if the script was fully functioning, the first player to use the command would turn it on, and the second player would turn it off.

stopEvent won't work the way your wanting like that.., and again even if you did stop the event '1000', it'd stop it for everyone.

ugh, I'll rewrite it.
Lua:
local c = {
    interval = 5,
    messages = {"Away!", "AFK!", "Not here!"},
    afk_players = {}
}

local function sendAfkText(cid)
    if isPlayer(cid) and c.afk_players[cid] = 1 then
        doSendAnimatedText(getThingPos(cid), c.messages[math.random(#c.messages)], 5914)
        addEvent(sendAfkText, c.interval * 1000, cid)
    else
        c.afk_players[cid] = 0
    end
end

function onSay(cid, words, param)
    if not c.afk_players[cid] then
        c.afk_players[cid] = 0
    end
    if c.afk_players[cid] == 1 then
        doPlayerSendCancel(cid, "Afk mode disabled.")
        c.afk_players[cid] = 0
        return true
    end
    c.afk_players[cid] = 1
    doPlayerSendCancel(cid, "Afk mode enabled.")
    addEvent(sendAfkText, 0, cid)
    return true
end
Code:
15:39 Foolish Knight [56893: !afk on
15:39 Foolish Knight [56893: doesnt work
with no error at console :S
 
Maybe someone has a better idea then using cid?
Add event id as value to a storage key, but not using globalstorage as you pointed out already. It's easy to fetch it that way (with getPlayerStorageValue) but will probably only work with inherent scripts related to its interface.

Code:
15:39 Foolish Knight [56893: !afk on
15:39 Foolish Knight [56893: doesnt work
with no error at console :S
You're probably not registering it correctly in talkactions.xml

Maybe something like this could work, as the event wont add again if there is no player to add it to. Also the first segment of function onSay will deal with potential error if player has logged out while being "afk"... I guess? Writing on my computer from work so cannot check it for you.
Lua:
local config = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}

local function sendText(cid)
    if isPlayer(cid) then
        doSendAnimatedText(getThingPos(cid), config.messages[math.random(#config.messages)], 5914)
        setPlayerStorageValue(cid, config.storage, addEvent(sendText, config.interval * 1000, cid))
    end
end

function onSay(cid, words, param)

    local key = getPlayerStorageValue(cid, config.storage)
    if key ~= -1 then
        doPlayerSendTextMessage(cid, 22, "You are now active.")
        setPlayerStorageValue(cid, config.storage, -1)
        stopEvent(key)
        return true
    end
   
    sendText(cid)
    doPlayerSendTextMessage(cid, 22, "You are now AFK.")
    return true
end
 
Last edited:
Add event id as value to a storage key, but not using globalstorage as you pointed out already. It's easy to fetch it that way (with getPlayerStorageValue) but will probably only work with inherent scripts related to its interface.


You're probably not registering it correctly in talkactions.xml

Maybe something like this could work, as the event wont add again if there is no player to add it to. Also the first segment of function onSay will deal with potential error if player has logged out while being "afk"... I guess? Writing on my computer from work so cannot check it for you.
Lua:
local config = {
    interval = 5,
    messages ={"Away!", "AFK!", "Not here!"},
    storage = 1000,
}

local function sendText(cid)
    if isPlayer(cid) then
        doSendAnimatedText(getThingPos(cid), config.messages[math.random(#config.messages)], 5914)
        setPlayerStorageValue(cid, config.storage, addEvent(sendText, config.interval * 1000, cid))
    end
end

function onSay(cid, words, param)

    local key = getPlayerStorageValue(cid, config.storage)
    if key ~= -1 then
        doPlayerSendTextMessage(cid, 22, "You are now active.")
        setPlayerStorageValue(cid, config.storage, -1)
        stopEvent(key)
        return true
    end
  
    sendText(cid)
    doPlayerSendTextMessage(cid, 22, "You are now AFK.")
    return true
end
not working else zzzz :(
 
Code:
15:39 Foolish Knight [56893: !afk on
15:39 Foolish Knight [56893: doesnt work
with no error at console :S
Well, your wrong about there not being an error.

change
Lua:
if isPlayer(cid) and c.afk_players[cid] = 1 then
Lua:
if isPlayer(cid) and c.afk_players[cid] == 1 then
 
Back
Top