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

Possible to add a delay in a loop?

strutZ

Australian OT Member {AKA Beastn}
Joined
Nov 16, 2014
Messages
1,393
Solutions
7
Reaction score
552
Hey there,

So I'm just experimenting with loops and i was wondering if someone could help me with this? Idea is to continually say 'AFK' if storage value is = to 1 BUT i want a 3 second delay before it goes through the loop again so it doesnt spam. (Like animated text)


Code:
--CONFIG--
local afkactive = 808012 -- Assign a unsued storage
--END CONFIG--


function onSay(cid, words, param)
local player = Player(cid)
    while(player:getStorageValue(afkactive) == 1)
        do
        if player:getStorageValue(748596123) < os.time() then
            player:setStorageValue(748596123, os.time() + 3)
            player:say('AFK', TALKTYPE_MONSTER_SAY)
        end
    end
end
 
Try this
Code:
--CONFIG--
local afkactive = 808012 -- Assign a unsued storage
--END CONFIG--

function onSay(cid, words, param)
local player = Player(cid)
    while(player:getStorageValue(afkactive) == 1 and os.date('*t', os.time()).sec % 3 == 0)
        do
        if player:getStorageValue(748596123) < os.time() then
            player:setStorageValue(748596123, os.time() + 3)
            player:say('AFK', TALKTYPE_MONSTER_SAY)
        end
    end
end
Although that might still spam it.
 
Try this
Code:
--CONFIG--
local afkactive = 808012 -- Assign a unsued storage
--END CONFIG--

function onSay(cid, words, param)
local player = Player(cid)
    while(player:getStorageValue(afkactive) == 1 and os.date('*t', os.time()).sec % 3 == 0)
        do
        if player:getStorageValue(748596123) < os.time() then
            player:setStorageValue(748596123, os.time() + 3)
            player:say('AFK', TALKTYPE_MONSTER_SAY)
        end
    end
end
Although that might still spam it.

mm didnt work and no errors in console.. I wonder if i can add an event or somthing... I even had a quick google.. seems a lot of people asking the same question lol

EDIT:
This is working

Code:
--CONFIG--
local afkactive = 808012 -- Assign a unsued storage
--END CONFIG--

function onSay(cid, words, param)
local player = Player(cid)
  while player:getStorageValue(afkactive) == 1 and player:getStorageValue(748596123) < os.time()
  do
  player:setStorageValue(748596123, os.time() + 3)
  player:say('AFK', TALKTYPE_MONSTER_SAY)
  end
end
But its only saying AFK once..
 
Last edited:
A talk action isn't consistent and i should of said that in the beginning, so use an onthink for this action instead of a while loop.

I never attempted to use onthink inside of an onsay, but i doubt it will work anyway, however you can set the storage value to be active in the talkaction and then have a creaturescript to see if the storage value is set using onthink.
 
A talk action isn't consistent and i should of said that in the beginning, so use an onthink for this action instead of a while loop.

I never attempted to use onthink inside of an onsay, but i doubt it will work anyway, however you can set the storage value to be active in the talkaction and then have a creaturescript to see if the storage value is set using onthink.
AN IDEA HAS BEEN STRUCK!
 
Its an example:
Talk action
Code:
--CONFIG--
local afkactive = 808012 -- Assign a unsued storage
--END CONFIG--

function onSay(cid, words, param)
   local player = Player(cid)
   if param == 'afk' and player:getStorageValue(afkactive) < 0 then
     player:setStorageValue(afkactive, 1)
   else
     player:setStorageValue(afkactive, -1)
   end
end
Creature script
Code:
local afkactive = 808012
local time_ = 0
function onThink(cid, interval)
   local player = Player(cid)
   if player:getStorageValue(afkactive) == 1 and (time_ * .001) % 3 == 0 then
     player:say('AFK', TALKTYPE_MONSTER_SAY)
   end
   time_ = time_ + interval
end
 
You should use addEvent. Try something like this. It's not strictly a loop, but work like one.

Code:
local afkactive = 808012

function repeatAfk()
   if player:getStorageValue(afkactive) == 1 then
     player:say('AFK', TALKTYPE_MONSTER_SAY)
     addEvent(repeatAfk(), 3*60*1000) -- 1000 = 1 second
   end
end

function onSay(cid, words, param)
local player = Player(cid)
   repeatAfk()
end
 
Last edited:
You should use addEvent. Try something like this. It's not strictly a loop, but work like one.

Code:
local afkactive = 808012

function repeatAfk()
   if player:getStorageValue(afkactive) == 1 then
     player:say('AFK', TALKTYPE_MONSTER_SAY)
     addEvent(repeatAfk(), 3*60*1000) -- 1000 = 1 second
   end
end

function onSay(cid, words, param)
local player = Player(cid)
   repeatAfk()
end
This is exactly what i was trying to do... Though it doesnt work for somereason.. no errors. RIP Brian hurts.
 
You sure setting the storage through some other shit?
Ye for now i jsut have anotehr talk action that is just '!afk on' which sets the storage.. and then i had the event as "!afk start" doesnt work.

If Mark could bring ack anitmatedText() that would be great haha
 
Ye for now i jsut have anotehr talk action that is just '!afk on' which sets the storage.. and then i had the event as "!afk start" doesnt work.

If Mark could bring ack anitmatedText() that would be great haha
What you mean as have it in an event? Try everything in one talk action. I just noticed I set it as 3 minutes instead of 3 seconds, edited now.
Code:
local afkactive = 808012

function repeatAfk()
   if player:getStorageValue(afkactive) == 1 then
     player:say('AFK', TALKTYPE_MONSTER_SAY)
     addEvent(repeatAfk(), 3*1000) -- 1000 = 1 second
   end
end

function onSay(cid, words, param)
local player = Player(cid)
   if player:getStorageValue(afkactive) ~= 1 then
     player:setStorageValue(afkactive, 1)
     repeatAfk()
   else
     player:setStorageValue(afkactive, 0)
   end
end
 
What you mean as have it in an event? Try everything in one talk action. I just noticed I set it as 3 minutes instead of 3 seconds, edited now.
Code:
local afkactive = 808012

function repeatAfk()
   if player:getStorageValue(afkactive) == 1 then
     player:say('AFK', TALKTYPE_MONSTER_SAY)
     addEvent(repeatAfk(), 3*1000) -- 1000 = 1 second
   end
end

function onSay(cid, words, param)
local player = Player(cid)
   if player:getStorageValue(afkactive) ~= 1 then
     player:setStorageValue(afkactive, 1)
     repeatAfk()
   else
     player:setStorageValue(afkactive, 0)
   end
end
Nope dont work and no errors.. haha its fucken dumb
 
Code:
function repeatAfk(id)
    local p = Player(id)
    if p then
        if p:getStorageValue(afkactive) == 1 then
            p:say('AFK', TALKTYPE_MONSTER_SAY)
            addEvent(repeatAfk, 3 * 1000, id) -- 1000 = 1 second
        end
    end
    return true
end

function onSay(player, words, param)
    if param == "on" then
        player:setStorageValue(afkactive, 1)
        repeatAfk(player:getId())
    elseif param == "off" then
        player:setStorageValue(afkactive, 0)
    end
    return true
end

Should work (Based on Peonso's)
 
Ok, that shit is kind of working. You will get a error in console if player log out while the event is setup to happen. And player that logged out afk will need to repeat "!afk" twice to put it up to work again.
<talkaction words="!afk" separator=" " script="afk.lua"/>
Code:
local afkactive = 808012

local function repeatAfk(cid)
   local player = Player(cid)
   if player:getStorageValue(afkactive) == 1 then
     player:say('AFK', TALKTYPE_MONSTER_SAY)
     addEvent(repeatAfk, 3000, cid)
     return false
   end
end

function onSay(player, words, param)
   if player:getStorageValue(afkactive) ~= 1 then
     player:setStorageValue(afkactive, 1)
     player:sendTextMessage(MESSAGE_INFO_DESCR, "You are now AFK.")
     repeatAfk(player.uid)
   else
     player:setStorageValue(afkactive, 0)
     player:sendTextMessage(MESSAGE_INFO_DESCR, "You are not AFK anymore.")
   end
   return false
end
 
Code:
function repeatAfk(id)
    local p = Player(id)
    if p then
        if p:getStorageValue(afkactive) == 1 then
            p:say('AFK', TALKTYPE_MONSTER_SAY)
            addEvent(repeatAfk, 3 * 1000, id) -- 1000 = 1 second
        end
    end
    return true
end

function onSay(player, words, param)
    if param == "on" then
        player:setStorageValue(afkactive, 1)
        repeatAfk(player:getId())
    elseif param == "off" then
        player:setStorageValue(afkactive, 0)
    end
    return true
end

Should work (Based on Peonso's)
You actually also solved both problems I got. Haha, I'm envy.
 
Back
Top