<talkaction words="!afk" separator=" " script="afk.lua"/>
local config = {
msg = 'AFK',
interval = 3000, -- in ms, 1000 ms = 1 s
storage = 12345 -- empty storage
}
local function sendAfkMessage(pid, startPos)
local player = Player(pid)
if not player then
return
end
if player:getStorageValue(config.storage) ~= 1 then
return
end
if player:getPosition() ~= startPos then
return player:toggleAFK()
end
local dir = player:getDirection()
player:setDirection(dir < 3 and dir + 1 or 0)
player:say(config.msg, TALKTYPE_MONSTER_SAY)
return addEvent(sendAfkMessage, config.interval, player.uid, startPos)
end
function Player:toggleAFK()
if self:getStorageValue(config.storage) ~= 1 then
self:setStorageValue(config.storage, 1)
self:sendCancelMessage('You have enabled AFK mode.')
sendAfkMessage(self.uid, self:getPosition())
else
self:setStorageValue(config.storage, 0)
self:sendCancelMessage('You have disabled AFK mode.')
end
end
function onSay(player, words, param)
player:toggleAFK()
return false
end
yes, next time just test the script and then if it don't work post on support and we can help youfor 1.3?
local config = {
talkAction = "!afk",
text = "AFK!",
interval = 3000,
storage = 9999,
canPushAfkPlayers = false
}
function TalkLoopAfk(pid, startPos)
local player = Player(pid)
if player and player:getStorageValue(config.storage) == 1 then
if startPos ~= player:getPosition() then
return TalkToogleAfk(player)
end
player:say(config.text, TALKTYPE_MONSTER_SAY)
local dir = player:getDirection()
player:setDirection(dir < 3 and dir +1 or 0)
addEvent(TalkLoopAfk, config.interval, pid, startPos)
end
end
function TalkToogleAfk(player)
if player:getStorageValue(config.storage) ~= -1 then
player:setStorageValue(config.storage, -1)
player:sendCancelMessage("You have disabled AFK mode.")
return false
end
player:setStorageValue(config.storage, 1)
player:sendCancelMessage("You have enabled AFK mode.")
TalkLoopAfk(player:getId(), player:getPosition())
return false
end
local talkAfk = TalkAction(config.talkAction)
talkAfk.onSay = TalkToogleAfk
talkAfk:register()
if not config.canPushAfkPlayers then
local ec = EventCallback
function ec.onMoveCreature(player, creature, fromPosition, toPosition)
if creature:isPlayer() and not player:getGroup():getAccess() then
if creature:getStorageValue(config.storage) ~= -1 then
player:sendCancelMessage("You cannot move player that are AFK!")
return false
end
end
return true
end
ec:register(-1)
end
Here you have a modification that does the same thing that was published here, but adds an extra mechanic which makes it so that nobody can move the player in AFK mode
data/scripts/afk.lua
Lua:local config = { talkAction = "!afk", text = "AFK!", interval = 3000, storage = 9999, canPushAfkPlayers = false } function TalkLoopAfk(pid, startPos) local player = Player(pid) if player and player:getStorageValue(config.storage) == 1 then if startPos ~= player:getPosition() then return TalkToogleAfk(player) end player:say(config.text, TALKTYPE_MONSTER_SAY) local dir = player:getDirection() player:setDirection(dir < 3 and dir +1 or 0) addEvent(TalkLoopAfk, config.interval, pid, startPos) end end function TalkToogleAfk(player) if player:getStorageValue(config.storage) ~= -1 then player:setStorageValue(config.storage, -1) player:sendCancelMessage("You have disabled AFK mode.") return false end player:setStorageValue(config.storage, 1) player:sendCancelMessage("You have enabled AFK mode.") TalkLoopAfk(player:getId(), player:getPosition()) return false end local talkAfk = TalkAction(config.talkAction) talkAfk.onSay = TalkToogleAfk talkAfk:register() if not config.canPushAfkPlayers then local ec = EventCallback function ec.onMoveCreature(player, creature, fromPosition, toPosition) if creature:isPlayer() and not player:getGroup():getAccess() then if creature:getStorageValue(config.storage) ~= -1 then player:sendCancelMessage("You cannot move player that are AFK!") return false end end return true end ec:register(-1) end
How about instead of hard coding the messages, players can put any message? (note: not sure if line #22 has the correct arguments order)Amazing script, I really like it! Provides good information for the times I really need to go afk.
By any chance can I request a minor modification?
The idea is, to add more messages to different parameters. For example: !afk 1, !afk 2, !afk 3.
This would allow me to insert different messages depending on the reason of why i'm afk.
Thanks in advance,
Regards!
local config = {
talkAction = "!afk",
prefix = "[AFK]: ",
interval = 3000,
storage = 9999,
canPushAfkPlayers = false
}
function TalkLoopAfk(pid, startPos, param)
local player = Player(pid)
if player and player:getStorageValue(config.storage) == 1 then
if startPos ~= player:getPosition() then
return TalkToogleAfk(player)
end
player:say(config.prefix .. param, TALKTYPE_MONSTER_SAY)
local dir = player:getDirection()
player:setDirection(dir < 3 and dir + 1 or 0)
addEvent(TalkLoopAfk, config.interval, pid, startPos)
end
end
function TalkToogleAfk(player, words, param)
if player:getStorageValue(config.storage) ~= -1 then
player:setStorageValue(config.storage, -1)
player:sendCancelMessage("You have disabled AFK mode.")
return false
end
player:setStorageValue(config.storage, 1)
player:sendCancelMessage("You have enabled AFK mode.")
TalkLoopAfk(player:getId(), player:getPosition(), param)
return false
end
local talkAfk = TalkAction(config.talkAction)
talkAfk.onSay = TalkToogleAfk
talkAfk:register()
if not config.canPushAfkPlayers then
local ec = EventCallback
function ec.onMoveCreature(player, creature, fromPosition, toPosition)
if creature:isPlayer() and not player:getGroup():getAccess() then
if creature:getStorageValue(config.storage) ~= -1 then
player:sendCancelMessage("You cannot move player that are AFK!")
return false
end
end
return true
end
ec:register(-1)
end