Little hunger system (trying to withdraw 20 each turn)

Hello,

I’ve been reading around for a while but I really require help now. I’m sorry if my set up of this piece of code is wrong (which is likely).

[lua]function WithdrawHunger()
local hunger = 100
if hunger == 100 then
hunger = 80
elseif hunger == 80 then
hunger = 60
end
Msg( tostring( hunger ) … "
" )
end
concommand.Add( “uz_WithdrawHunger”, WithdrawHunger )
timer.Create(“timer1”, 2, 5, RunConsoleCommand, “uz_WithdrawHunger”)[/lua]

What I am trying to achieve is that every turn this block of code is executed it will withdraw 20 from the variable hunger.
Although every time it is executed it displays 80 in console.

Thank you for looking and I would love any advice!

That would be because you are setting hunger every time you run your code. Try moving hunger out of the function.

Thank you very much! So logical!

EDIT:

One more question: Would it be possible to get the value of a variable from another file?
In this case hunger from init.lua and use it in hud.lua which is client sided?

The value hunger should update a little bar I made on the HUD. These calculations are made server sided so how do I use these calculations client sided?

include ( ‘init.lua’ ) in hud.lua and use the variable?

Thanks.

A little bump. I hope this is allowed after a day!

Do not include a serverside file in a clientside file, I did that once. Bad things happened.

serverside values can’t be read clientside. You have to network it to the client. Use NWInts or Usermessage it to the client. **

Usermessages**
**[Entity.SetNetworkedInt

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt)**

edit: Oh, and instead of doing a bunch of if checking, just do

[lua] hunger=hunger-20 [/lua]

Damnit, that’s what I was going to say >:( Beat me to it.

Sorry but that code is just horrid.

If you told me what you are doing i could rescript it?

Thanks but I want to learn it the hard way. Could you give me advice about what I did wrong anyways? Thanks.

All others thank you as well I’ll look into the NWints and usermessage.