Hello again. I was looking to make a variable for a player, for example, Hunger, that starts to 100, and it goes down to 0. I need a timer for that, I know, but what I don’t know is how I can make the player get hurt every second, when the hunger is 0.
I hope you can help me, any constructive reply will be appreciated.
I just tried to put that piece of code into init.lua, in GM:Think function and outside the function. It started to show errors like “expected symbol near “(”” and when I put a “)” in the timer.Create thing, it shows an error "unexpected symbol near “(”, and things like that. Also it crashes the whole init thing. And also, in the SetHealth stuff, it makes an error of the “-=” thing. Help!
Note: I already have a way to put temporal values.
[lua]function GM:Think() for k, v in pairs( player.GetAll() ) do if ( !v.fHungerDelay ) then v.fHungerDelay = CurTime() + (360); //Every 3 minutes if ( CurTime() < v.fHungerDelay) then return; end //Hunger subtracting stuff v.fHungerDelay = CurTime() + (360) endend [/lua]
There is probable a better way to do this than to loop through all players
[lua] for k, v in pairs( player.GetAll() ) do
if ( !v.fHungerDelay ) then v.fHungerDelay = CurTime() + (110); //Every 3 minutes
if ( CurTime() > v.fHungerDelay) then --Hunger subtracting stuff
if v.Hunger == 0 then
v:SetHealth(v:Health() - 20)
v.fHungerDelay = CurTime() + (110)
When the Hunger value reaches 0, it doesn’t hurts the player (made the dmg intervals beetween 10 secs to make it faster while testing.)
I could be wrong as I am a beginner, but your HUD will work with the subtracting of the hunger because they are both in your client. But the you need to send that info to the server in order for it to know when to damage you.
Why does it take 16 posts to write a working timer for him.
timer.Create(“SubtractHunger”, 1, 0, function()
for k,v in pairs(player.GetAll()) do
if v:Alive() then
v.hunger = v.hunger - 1
if v.hunger <= 0 then
v.hunger = 100
end)[/lua] That should get you going in the serverside part.