• How to do a Cooldown?
    5 replies, posted
I want to make an "if" function have a cooldown time before it can execute again, but i don't have an idea how i could do this. example code [code]function GM:PlayerHurt(victim, attacker) if victim:Health() <30 then victim:EmitSound("hurt.wav") end end[/code] This code would play the sound every time the player get hit when his health is below 30. How can i make it have a cooldown of x seconds before it can play the sound again??
how the hell should I know I sell boats for christ sake
Try something like this: [code] function GM:PlayerHurt(victim, attacker) if ( victim:Health() < 30 and CurTime() - ( victim.previousHurtSoundTime or 0 ) >= 2 ) then -- once every 2 seconds victim:EmitSound("hurt.wav") victim.previousHurtSoundTime = CurTime() end end [/code]
[QUOTE=DEFCON1;44505925]Try something like this: [code] function GM:PlayerHurt(victim, attacker) if ( victim:Health() < 30 and CurTime() - ( victim.previousHurtSoundTime or 0 ) >= 2 ) then -- once every 2 seconds victim:EmitSound("hurt.wav") victim.previousHurtSoundTime = CurTime() end end [/code][/QUOTE] on server sartup this code causes an lua error in my test. TEST [lua/autorun/weaponremover.lua][lua/autorun/codhealthsys.lua] [ERROR] lua/autorun/codhealthsys.lua:75: 'then' expected near ')' because i don't understand this part of the code i can't find the mistake myself.
I've just tried this code and it works as expected. You made an error while copy/pasting, or the error is somewhere else above this code. Post the first 75 lines of your script.
yes it was a mistake of mine. thanks for the help
Sorry, you need to Log In to post a reply to this thread.