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

Lua 2 questions about function onThink(cid, interval)

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
I want to ask two questions about function onthink for creaturescripts.
1. Does it lag server each 500 ms?
For example:
Code:
function onThink(cid, interval)
     print(interval)
     return true
end
2. Is it possible to stop that function? If it is, then how to?
Thanks. :)
 
I don't think onThink is for creaturescripts.
I'm fairly sure it's for globalevents.

1. Depends on what your checking.
If it's only doing what you have in the script at the moment, then no it won't lag.

2. I don't think it's possible.
You can add a check before it starts checking for various things, like the os.time, or a globalstorage, to determine if the onThink is required to do something, but otherwise it's going to continue doing it's check every interval you select in globalevents.xml
 
onThink creaturescripts are only activated while they are registered.
If you do this: creature:unregisterEvent("nameOfEvent") it will no longer be executed.

However what I usually do is what Xikini already suggested:
You can add a check before it starts checking for various things, like the os.time, or a globalstorage, to determine if the onThink is required to do something

You can only reduce the interval trough TFS (currently its 1000 msec)
It wont necessarily lag the server, but depends what its suppose to do.

Depending what you are trying to do, maybe you want to use addEvent() inside the onThink.
This way it wont clog up the process and kinda still run the onThink scripts more than 1 time per sec.

I have created auras for Whi World what sends disdance effects around player 4 times per sec. I did it with addEvents.
 
I've created animations on position x, y , z. Those animations uses onThink, because it must be repeated each 1 second. I hope it does not lag.
 
i do it this way
Code:
local buffBgHealPos = {x = 869, y = 1064, z = 7}
local buffBgHealStorage = 29740
local buffBgSpeedPos = {x = 888, y = 1084, z = 7}
local buffBgSpeedStorage = 31741
local buffBgSpeedBluePos = {x = 876, y = 1137, z = 6} -- mana
local buffBgSpeedBlueStorage = 31742

local buffBgManaBluePos = {x = 887, y = 1107, z = 7} -- speed
local buffBgManaBlueStorage = 31743

function onThink(cid, interval)
   
    if lastinterval == nil then
        lastinterval = os.time()
    end

    if (os.time() - lastinterval) > 1 then
    
        if (os.time() > getGlobalStorageValue(buffBgHealStorage) + 120) then
            doSendMagicEffect(buffBgHealPos, 54)
        end
        if (os.time() > getGlobalStorageValue(buffBgSpeedStorage) + 60) then
            doSendMagicEffect(buffBgSpeedPos, 18)
        end
        if (os.time() > getGlobalStorageValue(buffBgManaBlueStorage) + 120) then
            doSendMagicEffect(buffBgManaBluePos, 6)
        end
        if (os.time() > getGlobalStorageValue(buffBgSpeedBlueStorage) + 70) then
            doSendMagicEffect(buffBgSpeedBluePos, 4)
        end

       
        lastinterval = os.time()
       
        return true
    end
   
    return false
end

move

Code:
local function speedToNormal(cid)
    doChangeSpeed(cid, -3000)
    return true
end

function onStepIn(cid, item, fromPos, item2, toPos)

    if item.uid == buffBgHealRedUID then
        if (os.time() > getGlobalStorageValue(buffBgHealStorage) + 120) then -- -- jeśli teraz jest > od 15:06
            doCreatureAddHealth(cid, getCreatureMaxHealth(cid), false)
            doSendMagicEffect(getThingPos(cid), 14)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, 'Your health has been regenerated. Buff will spawn again in 2 minutes.')
            setGlobalStorageValue(buffBgHealStorage, os.time()) -- 15:00
        end
    end
    if item.uid == buffBgSpeedRedUID then
        if (os.time() > getGlobalStorageValue(buffBgSpeedStorage) + 60) then -- -- jeśli teraz jest > od 15:06
            doChangeSpeed(cid, 3000)
            addEvent(speedToNormal, 15*1000, cid)
            doSendMagicEffect(getThingPos(cid), 13)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, 'You\'re faster for 15 seconds. Buff will spawn again in 1 minute.')
            setGlobalStorageValue(buffBgSpeedStorage, os.time()) -- 15:00
        end
    end
    if item.uid == buffBgManaBlueUID then
        if (os.time() > getGlobalStorageValue(buffBgManaBlueStorage) + 120) then -- -- jeśli teraz jest > od 15:06
            doCreatureAddMana(cid, getCreatureMaxMana(cid), false)
            doSendMagicEffect(getThingPos(cid), 12)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, 'Your mana has been regenerated. Buff will spawn again in 2 minutes.')
            setGlobalStorageValue(buffBgManaBlueStorage, os.time()) -- 15:00
        end
    end
    if item.uid == buffBgSpeedBlueUID then
        if (os.time() > getGlobalStorageValue(buffBgSpeedBlueStorage) + 70) then -- -- jeśli teraz jest > od 15:06
            doChangeSpeed(cid, 3000)
            addEvent(speedToNormal, 17*1000, cid)
            doSendMagicEffect(getThingPos(cid), 13)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, 'You\'re faster for 17 seconds. Buff will spawn again in 1 minute and 10 seconds.')
            setGlobalStorageValue(buffBgSpeedBlueStorage, os.time()) -- 15:00
        end
    end
   
    return true
end
 
the effects are only every 60-120 seconds?
you could just make a function that takes effect/position/delay as arguments, and use addEvent for that on startup
e.g
Code:
sendeffects(effect, position, delay)
doSendMagicEffect(position, effect)
addEvent(sendeffects, delay, effect, position)
end
and in startup.lua
sendeffects(effect, position, delay)
 
the effects are only every 60-120 seconds?
you could just make a function that takes effect/position/delay as arguments, and use addEvent for that on startup
e.g
Code:
sendeffects(effect, position, delay)
doSendMagicEffect(position, effect)
addEvent(sendeffects, delay, effect, position)
end
and in startup.lua
sendeffects(effect, position, delay)
yes, i understand functions, but still thanks, y
 
the effects are only every 60-120 seconds?
you could just make a function that takes effect/position/delay as arguments, and use addEvent for that on startup
e.g
Code:
sendeffects(effect, position, delay)
doSendMagicEffect(position, effect)
addEvent(sendeffects, delay, effect, position)
end
and in startup.lua
sendeffects(effect, position, delay)
I would not use addEvents like that.
Sometimes with no reason it just stops and then you can only manually start it up or startUp server again.

Use globalevents for the first script, because you are not using cid anywhere.
 
I would not use addEvents like that.
Sometimes with no reason it just stops and then you can only manually start it up or startUp server again.

Use globalevents for the first script, because you are not using cid anywhere.
never had that issue with events o_o

also as crazy said addEvent returns event id, so you could store that somewhere and periodically check if the effect events are active to counter that
 
Last edited:
Back
Top