• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Solved creature interval and os.time [TFS 1.2]

Aeronx

Intermediate OT User
Joined
Dec 17, 2015
Messages
746
Solutions
9
Reaction score
125
Hello otlanders. I've started doing some testing with party buffs and such.

And I got to this.

Code:
local times = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    function shine9(cid)
        local cid = Creature(cid)
        local position = cid:getPosition()
        position:sendMagicEffect(19)
    end

    function storagesnil(cid)
        local cid = Creature(cid)
        cid:setStorageValue(60245, cid:getStorageValue(60245)-10)
        cid:setStorageValue(6902, 0)
        cid:unregisterEvent('partycrit')
    end

function onThink(cid, interval)
    if last_interval == nil then
          last_interval = os.time()
    end
    if ((os.time()) - last_interval) > 25 then
           cid:setStorageValue(60245, cid:getStorageValue(60245) + 10)
           cid:setStorageValue(6902,1)
           addEvent(storagesnil, 25 * 1000, cid.uid)
           for i = 1, #times do
                 addEvent(shine9, times[i] * 2500, cid.uid)
           end
           last_interval = os.time()
     end
return false
end

What does this do? When I cast a spell i register this event to all party members or to caster if not party.

If there no party. It works perfectly! The problem comes with party. Let's say there are 3 members on the party, after casting the spell, it registers to all at the same time BUT only player1 of the party will get the buff for 25 seconds, after 25 seconds, player2 will get the buff, and after 25 seconds, player3.

I've been doing some testing and tested with interval > 1.

If i do that, player1 get like tons of executions of this function, after 1 second player2 starts getting many executions of this function and so on.

If i dont set an interval check "if ((os.time()) - last_interval) > 25 then", they all get the buff at the same time and many executions of the function.

What i am trying to accomplish? Execute this function once for all players affected at same time.

I understand the idea behind the os.time with the storage. Just trying to learn a bit here.

So how should i approach this to accomplish what i am looking for? All explanations and exemples are more than welcome.

Thank you for your time!
 
Solution
The onThink function repeats with a set interval, so without the line
Code:
if ((os.time()) - last_interval) > 25 then
it will execute a lot of times in that set interval.

You changed the interval to 1 and removed the check to test, but you need to change the unregisterEvent too, without the check, you can just call the function storagesnil, with the check you need to put the same time in the storagesnil function addEvent. If the interval is 1, and the unregisterEvent will be called in 25 seconds, it will keep running.
The shine addEvent change its time for each value in the table so it would need to be changed too.

So you can try to execute it once and unRegister to stop the many executions or as you mentioned, using storage with...
The onThink function repeats with a set interval, so without the line
Code:
if ((os.time()) - last_interval) > 25 then
it will execute a lot of times in that set interval.

You changed the interval to 1 and removed the check to test, but you need to change the unregisterEvent too, without the check, you can just call the function storagesnil, with the check you need to put the same time in the storagesnil function addEvent. If the interval is 1, and the unregisterEvent will be called in 25 seconds, it will keep running.
The shine addEvent change its time for each value in the table so it would need to be changed too.

So you can try to execute it once and unRegister to stop the many executions or as you mentioned, using storage with os.time so you could keep the same structure.
 
Last edited:
Solution
Back
Top