[LUA Scripting Issue] Hunger functions.

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.

timer.Create( 1, 0, function() --Updates everysecond
	if ( HungerVar ) == 0 then --Makes sure their hunger is equal to zero
		ply:SetHealth( ply:Health -= 5 ) --Damages Player for 5 HP

There’s a simple one using a timer. Just keep in mind if you use a NWInt for your Hunger variable, then prepare to face some server lag.

I would make a timer that, once finished, started another timer that would run 50 times, and every time it ran it would damage the player for 2 health. Not sure how that would turn out.

What do you recommend me to call a variable without using NWInt? And also, where I can put this file, I mean, in which gamemode file (cl_init, init, etc).
Also, thanks for the reply.

This function would go in one of your server-side files, such as the init.lua, and you can use a NWInt, it may lag if a lot of players are on.

In what function? GM:Think? Which?

[editline]28th January 2013[/editline]

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.

I would recommend GM:Think and make a delay variable instead of a timer

How I can make a delay variable?

[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

Im having some troubles applying this piece of code to the gamemode, but maybe with some patience im able to make it work.

don’t make the for loop keep checking, put the !v.fHungerDelay before the for loop (if then for k,v etc)

If I did that then ‘v’ would return nil thus causing and error

[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() + (1

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.)

Are you using it in a think function?

Are you subtracting the hunger value, and how?

Yes, Im using it on the Think functions. And I substract Hunger in a cl_init file… maybe not the correct, because is just client, but in the hud it seems to be working. I use a timer:

function DecHunger()
if plyHunger > 0 then plyHunger = plyHunger - 1 end
timer.Simple(1, DecHunger)
timer.Simple(1, DecHunger)

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.

Never mind, misread

Thank you to everyone who tried to help. I already could find a way to make a Hunger system.