Having trouble implementing hunger

Hello. I’m creating a survival game mode, and most survival-type games tend to have the element of “hunger” in them.
You get hungry over time, then once you get far too hungry, you start dieing pretty quickly. You can eat food to restore your hunger level.
I’m trying to recreate this, but I’m honestly not sure about how to go about doing so.
The specific parts I’m stuck on is the damage dealing part once they have reached 0 hunger (I’m declaring a variable “hunger” and setting it to 100 on player spawn).
If someone could help me, that would be awesome. Thanks for reading.

Where are you decreasing the hunger?



function DecreaseHunger(ply)

if(hunger <= 0) then

ply:Kill()

else

hunger = hunger - 1

print("Hunger: " .. hunger)

end

end


I then have a timer at the bottom of init.lua:



timer.Create("Hunger", 30, 0, DecreaseHunger)


I also change the variable “hunger” to 100 in the player spawn function.

I would give you an example but I have to go back to school in 1 minute so, just check back in this thread in about 4 hours.

Why’d you disagree? Is there something I’m doing wrong or is this method of doing things bad? I would like to know as I’m obviously looking to improve on my LUA skills (I’m still a novice)

EDIT: Saw your post, awaiting reply.

Sorry this took so long, had other things to do instead.
[lua]
local Player = FindMetaTable(“Player”)
if !Player then return end

if SERVER then
AddCSLuaFile(“autorun/HungerMod.lua”)
util.AddNetworkString(“Player_SendHunger”)
function Player:SetHunger(num)
net.Start(“Player_SendHunger”)
net.WriteInt(num, 16)
net.Send(self)
self.Hunger = num
end

function Player:AddHunger(num)
	self:SetHunger(self:GetHunger() + num)
end

function Player:HungerDecrease(amt,delay)
	self:HungerDecreaseStop()
	local time = delay or 30
	timer.Create("Hunger_Decrease"..self:UniqueID(), time, 0, function()
		if self:GetHunger() &gt; 0 then
			self:AddHunger(amt)
		else
			self:TakeDamage(15, self, self)
		end
	end)
end

function Player:HungerDecreaseStop()
	if timer.Exists("Hunger_Decrease"..self:UniqueID()) then
		timer.Remove("Hunger_Decrease"..self:UniqueID())
	end
end

hook.Add("PlayerInitialSpawn", "Hunger.PlayerInitialSpawn", function(ply)
	if IsValid(ply) then
		timer.Simple(1, function()
			ply:SetHunger(100)
			ply:HungerDecrease(-1,10)
		end)
	end
end)

hook.Add("PlayerSpawn", "Hunger.PlayerSpawn", function(ply)
	ply:SetHunger(100)
end)

else
net.Receive(“Player_SendHunger”, function(len)
local num = net.ReadInt(16)
LocalPlayer().Hunger = num
end)

hook.Add("HUDPaint", "Hunger.HUDPaint", function()
	local ply = LocalPlayer()
	local hunger = ply:GetHunger()
	local status = ply:HungerStatus()
	if IsValid(ply) and hunger and status then
		draw.SimpleText(hunger, "Default", 10, 10, Color(255,255,255,255))
		draw.SimpleText(status, "Default", 10, 30, Color(255,255,255,255))
	end
end)

end

local Hunger_Status = {
{100, “Full”},
{90, “Stuffed”},
{80, “Satisfied”},
{70, “Getting Hungry”},
{60, “Needs Food”},
{50, “Starving”},
{40, “Will eat anything”},
{20, “Dying”},
{0, “Gonna pass out”}
}

function Player:HungerStatus()
local amt = self:GetHunger()
for k,v in pairs(Hunger_Status) do
if amt >= v[1] then
return v[2]
end
end
end

function Player:GetHunger()
return self.Hunger or 0
end
[/lua]

Tell me if you have any problems with it.

-snip-

I shall test it now.

[HR][/HR]

For some reason, the HUDPaint hook is not working (none of the code is being run?). I am not completely sure if this is the problem, but I already have a HUDPaint hook in my cl_hud.lua file, can you have two identical hooks working at the same time?
The timer is also not working correctly:


    function Player:HungerDecrease(amt,delay)
        self:HungerDecreaseStop()
        local time = 10
        timer.Create("Hunger_Decrease"..self:UniqueID(), time, 0, function()
            if self:GetHunger() > 0 then
                self:AddHunger(amt)
				print("worked")
            else
                self:TakeDamage(15, self, self)
            end
        end)
    end

After ten seconds, the message is NOT printed in the console.

First of all, this is a shared file meaning you have to include it and addcslua it serverside then include it again clientside.

Second, I don’t know what you’re doing but the script I gave you works perfectly in a LAN server and such hunger is taken.

You’re most likely doing something wrong on your side.

OK, thanks for the reply. My setup was adding the lua file server-side, including it server-side, and including it client side. I also took out the 5th line of the script (AddCSLuaFile). Will try again.

Wouldn’t it cause a problem if a player disconnects? The timer would still reach 0, then it would do:

if self:GetHunger() > 0 then

but self would be nil as the player doesn’t exist…

Just do your own checks then.

[lua]
– Initialization
function HungerInit( ply )

ply.hunger = 100

timer.Create( ply:UniqueID() .. "hunger" , 10, 0, function()
	if ply:Alive() then
		
		local nextHunger = ply.hunger - math.Rand( 1, 2 )
		
		if ply:Alive() then ply:Kill() end
			ply.hunger = 0;
		elseif nextHunger &gt; 100 then
			ply.hunger = 100
		else
			ply.hunger = nextHunger;
		end
		
	end
end )

end
hook.Add( “PlayerInitialSpawn”, “HungerInit”, HungerInit )

– Reset on player spawn
function HungerSet( ply )

ply.hunger = 100

end
hook.Add( “PlayerSpawn”, “HungerSet”, HungerSet )

– Remove on player deletion.
function HungerRemove( ent )

if ent:IsPlayer() then
	timer.Remove( ent:UniqueID() .. "hunger" );;
end

end
hook.Add( “EntityRemoved”, “HungerRemove”, HungerRemove )
[/lua]

[editline]25th January 2013[/editline]

This is all serverside, change hunger to a networked var if you want it networked.