Hunger System

I’ve never really tried to make a hunger type of system so most of it is just trial and error I guess, I was told to do it based off movement etc so Im using a timer if the player is moving for 5 minutes and stops when they stop moving (code has problems) so I need a little help I guess.

  1. If the player stops. The timer will completely pause and I need to start it again once they keep moving
  2. I get this error in the init.lua (im using a PlayerSpawn hook)

[ERROR] gamemodes/wasteland/gamemode/init.lua:24: attempt to call method 'StartHunger' (a nil value)

I’m not getting very far at this right now so all help is appreciated.

local meta = FindMetaTable("Player")

function meta:StartHunger(ply)

	local CPlayer = LocalPlayer()

	if ply:GetVelocity():Length() > 0 then
		timer.Create("Hunger", 300, 0, function()

				elseif ply:GetVelocity():Length() =< 0 then
					print("Timer Paused ")



Im still not sure whether I need the player object for this too. Could I just do it all serverside? (Not very good at this stuff)

Here is an example hunger system I wrote and posted here to help someone else.


Another example which requires there to be a person on TEAM_COOK:

Hopefully that’ll help get you started. Try to think about is as a way to manage a variable. When the variable is at x then start hurting.

Then decide how you want to manipulate the variable. If you want it to go down over time you can use timers, or cooldown vars until time to remove a point. How about resetting on spawn, ensuring the value can only go between a range of say 0-100 or 0-1…

If you want it to change based on movement, why not use some modifier based on the player speed over walk-speed ( walking doesn’t cost much in terms of energy but sprinting / running would cost more ), so a modifier 0-1 based on current speed over walk-speed where 0 is still walk-speed and 1 is when the player is moving at >= max run speed. Then apply the modifier to some cost element of x energy to use per second if at full sprint, the 0-1 modifier applied to that would give a linear up-hill climb of energy to use.

Then decide how you want to reduce hunger ( food, drink, etc… ) and if you want a long-term hunger type system using a stamina system built in so that running may drain energy temporarily but still cost more in terms of hunger but it won’t let you run until you’re starving…

There are lots of ways to take it.