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

onThink problems

bomba

Member
Joined
Feb 26, 2008
Messages
635
Reaction score
7
Location
Brazil
Why sometimes works and sometimes does not?

Code:
function onThink(cid, interval)
    if last_interval == nil then
      last_interval = os.clock()
    end
    if (os.clock() - last_interval) > 2 then        --        execute every 2 sec
        if getPlayerStorageValue(cid, eSkill.charm) >= 1 then
          doCreatureAddHealth(cid, getPlayerStorageValue(cid, eSkill.charm))
          doCreatureAddMana(cid, getPlayerStorageValue(cid, eSkill.charm))
        end       
      last_interval= os.clock()
          return true
    end
  return false
end

I want to to work every 2 seconds.
 
Try this:
PHP:
function onThink(cid, interval)
    if time_check <= 0 then
        time_check = os.time()
    end
    if time_check <= os.time() then
        if getPlayerStorageValue(cid, eSkill.charm) >= 1 then
          doCreatureAddHealth(cid, getPlayerStorageValue(cid, eSkill.charm))
          doCreatureAddMana(cid, getPlayerStorageValue(cid, eSkill.charm))
        end     
        time_check = os.time()+2
    end
return true
end
 
Can I ask why do you use/need internal time checks instead of the interval on the xml?
it's an onThink function, so it's most likely in an npc script..

and this "doCreatureAddHealth(cid, getPlayerStorageValue(cid, eSkill.charm))" tells me something might happen with your life while chatting with an npc or something..
What do we know.. we should embrace custom stuffs.

Beside, if this would be something like a globalevent.. I can tell you not all servers have globalevents, and not everyone is skilled enough to know how to add that.
NPC's have for a long time been the easy way of making globalevents :)

I think we should give the support on what they ask for, not question their way of handling it.
The day this guy ask how to add globalevents or similar on C++ level, I'd be happy to help him as well :)

or do you disagree on what I had to say?
 
Yeah I know but with more info is easier to help...
I agree, but sometimes they are so new to the stuffs, they they themselves can't say what's the problem.. they have no clue what to say..

But I must say people who want help, and try to hide something at same time are very annoying.
etc modifying the scripts they post here they want help with, just to hide something.
and they give an error.. that's invalid because the error in the script switch line because something was removed :P
 
this is a creaturescript
onThink creaturescript...

Look the .xml file:
Code:
    <event type="think" name="Charm" event="script" value="charm.lua"/>
 
2ec44gp.png

Code:
function onThink(cid, interval)
    if time_check <= 0 then
        time_check = os.time()
    end
    if time_check <= os.time() then
        if getPlayerStorageValue(cid, eSkill.charm) >= 1 then
          doCreatureAddHealth(cid, getPlayerStorageValue(cid, eSkill.charm))
          doCreatureAddMana(cid, getPlayerStorageValue(cid, eSkill.charm))
        end   
        time_check = os.time()+2
    end
return true
end
 
Use it as a globalevent, is going to execute every 2 seconds (configurable on the xml)

Charm.lua
Code:
function onThink(cid, interval)
for _, cid in pairs(getPlayersOnline()) do
  if getPlayerStorageValue(cid, eSkill.charm) >= 1 then
    doCreatureAddHealth(cid, getPlayerStorageValue(cid, eSkill.charm))
    doCreatureAddMana(cid, getPlayerStorageValue(cid, eSkill.charm))
  end
end
return true
end

Globalevents.xml
Code:
<globalevent name="Charm" interval="2000" event="script" value="charm.lua"/>

Anyway, do you have the eSkill tables on the libs? Otherwise is gonna print a lot of errors.
 
Thx Colors, but I edited to:
Code:
function charmOn(cid)
    if isPlayer(cid) == true then
        if getPlayerStorageValue(cid, eSkill.charm) >= 1 then
          doCreatureAddHealth(cid, getPlayerStorageValue(cid, eSkill.charm))
          doCreatureAddMana(cid, getPlayerStorageValue(cid, eSkill.charm))
          addEvent(charmOn, 2*1000, cid)
        end
    end
  return true
end

function onLogin(cid)
    if getPlayerStorageValue(cid, eSkill.charm) >= 1 then
      addEvent(charmOn, 2*1000, cid)
        end  
  return true
end
 
Back
Top