Health doesn't decrease after the player has died once already

Hello everyone!! I’m trying to make a hunger system where if your hunger goes to 0 then your health starts decreasing until you eventually die.

The problem is, after the players hunger goes to 0 and they die, the next time they respawn, their health doesn’t start decreasing after their hunger goes to 0.

Here is a video of what I mean:

robloxapp-20220314-1822544.wmv (4.9 MB)

I’ve tried printing the humanoid/health etc. and everything printed perfectly. I’m clueless as of why it doesn’t work.

Here is the script, it’s a little long, but I think the only part that matters is after line 128 where I commented “IT DOESN’T WORK HERE”.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

local Remotes = ReplicatedStorage:WaitForChild("Events")
local MainEvent = Remotes:WaitForChild("Eat")

-- HUNGER

local MAX_HUNGER = 100
local STARTING_VALUE = MAX_HUNGER
local DECREASE_AMOUNT = 20
local HUNGRY_AMOUNT = 50
local MIN_HUNGER = DECREASE_AMOUNT
local DECREASE_TIME = 2
local START_DECREASE_TIME = DECREASE_TIME * 2

-- SATURATION

local SATURATION_STARTING_VALUE = 0
local SATURATION_DECREASE_AMOUNT = 1
local SATURATION_DECREASE_TIME = DECREASE_TIME / 4
local MAX_SATURATION = 100
local MIN_SATURATION = SATURATION_DECREASE_AMOUNT

-- STARVE

local STARVE_INTERVAL = 0.5
local STARVE_DAMAGE = 10

-- EFFECTS

local DEFAULT_SPEED = 16
local SOUR_SPEED = 20
local SOUR_INTERVAL = 5
local EAT_INTERVAL = 3


local DEBOUNCE = false

--

local Foods = {
	["Carrot"] = {["Sour"] = false, ["HungerIncrement"] = 2, ["Saturate"] = 20},
	["Apple"] = {["Sour"] = true, ["HungerIncrement"] = 3, ["Saturate"] = 35},
	["Lemon"] = {["Sour"] = true, ["HungerIncrement"] = 1, ["Saturate"] = -10},
	["Orange"] = {["Sour"] = true, ["HungerIncrement"] = 3}, ["Saturate"] = 30,
}

local Drinks = {
	["Soda"] = {["Sweet"] = true, ["SpeedIncrement"] = 5},
	["Lemonade"] = {["Sweet"] = false, ["SpeedIncrement"] = 3},
	["Orange Juice"] = {["Sweet"] = true, ["SpeedIncrement"] = 4},
}

--

local Full = false
local Hungry = false
local Starving = false
local Saturated = false

local State = nil

function RBX_ASSET_ID(number)
	if number then
		number = tostring(number)
		return "rbxassetid://".. number
	end
	return false
end

function new(Player)
	-- Check if there is a player data folder, if not, create one
	local PlayerData = nil
	if not Player:FindFirstChild("playerData") then
		PlayerData = Instance.new("Folder")
		PlayerData.Name = "playerData"
		PlayerData.Parent = Player
	else PlayerData = Player:WaitForChild("playerData") end

	-- Create the hunger value
	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Value = STARTING_VALUE
	Hunger.Parent = PlayerData

	-- Create the saturation value
	local Saturation = Instance.new("IntValue")
	Saturation.Name = "Saturation"
	Saturation.Value = SATURATION_STARTING_VALUE
	Saturation.Parent = PlayerData

	-- Set attributes so variables can be accessed by other scripts

	local function setAttributes(HUMANOID)
		if HUMANOID then
			HUMANOID:SetAttribute("MaxHunger", MAX_HUNGER)
			HUMANOID:SetAttribute("MinHunger", MIN_HUNGER)
		end
	end
	
	
	local Character = Player.Character
	if not Character or not Character.Parent then
		Character = Player.CharacterAdded:Wait()
	end
	local Humanoid = Character:WaitForChild("Humanoid")
	
	setAttributes(Humanoid)

	-- 

	task.wait(START_DECREASE_TIME) -- Wait the start decrease time amount before the players health starts decreasing
	
	while task.wait() do
		-- HUNGER (Uses two if statements to check if saturated to make the code look less messy)
		if not Saturated then
			if Hunger.Value >= MIN_HUNGER then
				-- DECREASE
				task.wait(DECREASE_TIME)
				Full = false
				Hunger.Value -= DECREASE_AMOUNT
				if Hunger.Value <= HUNGRY_AMOUNT then
					Hungry = true
				end
			else
				-- IT DOESN'T WORK HERE
				-- STARVING
				task.wait(STARVE_INTERVAL)
				Starving = true
				Hunger.Value = 0 -- Makes sure that the hunger cannot go less than 0
				if Humanoid then
					Humanoid:TakeDamage(STARVE_DAMAGE)
				end
			end
		end
		-- SATURATION
		if Saturated then
			if Saturation.Value >= MIN_SATURATION then
				task.wait(SATURATION_DECREASE_TIME)
				Saturation.Value -= SATURATION_DECREASE_AMOUNT or 2
			else Saturation.Value = 0 Saturated = false end
			if Saturation.Value - SATURATION_DECREASE_AMOUNT == MIN_SATURATION then
				task.wait(DECREASE_TIME)
				Saturation.Value = 0
			end
		end
		
		Humanoid.Died:Connect(function()
			Saturation.Value = MAX_SATURATION
			Hunger.Value = MAX_HUNGER
		end)
	end
end


Players.PlayerAdded:Connect(new)

function Activated(Player, Name, FoodInstance)
	-- Define the players character
	local Character = Player.Character
	if not Character or not Character.Parent then
		Character = Player.CharacterAdded:Wait()
	end
	local Humanoid = Character:WaitForChild("Humanoid")
	-- Food
	if Foods[Name.Value] then
		local Food = Foods[Name.Value]
		local PlayerData = Player:FindFirstChild("playerData")
		local Hunger = PlayerData:FindFirstChild("Hunger")
		local Saturation = PlayerData:FindFirstChild("Saturation")
		if PlayerData and Hunger and not DEBOUNCE then
			DEBOUNCE = true
			-- Eating animation
			if FoodInstance then
				local Animator = Humanoid:WaitForChild("Animator")

				local EatAnimation = Instance.new("Animation")
				EatAnimation.AnimationId = RBX_ASSET_ID(9039489111)

				local EatAnimationTrack = Animator:LoadAnimation(EatAnimation)
				EatAnimationTrack:Play()
				Debris:AddItem(FoodInstance, 3)
				task.wait(EAT_INTERVAL) DEBOUNCE = false
			end
			-- Increase hunger
			if not (Hunger.Value + Food["HungerIncrement"] >= MAX_HUNGER) then
				Hunger.Value += Food["HungerIncrement"]
			else Hunger.Value = MAX_HUNGER Full = true end
			-- Increase saturation
			if Food["Saturate"] then
				Saturated = true
				if not (Saturation.Value + Food["Saturate"] >= MAX_SATURATION) then
					Saturation.Value += Food["Saturate"]
				else Saturation.Value = MAX_SATURATION end
			end
			-- Check if the food is sour
			if Food["Sour"] then
				if Humanoid.WalkSpeed == SOUR_SPEED then repeat task.wait() until Humanoid.WalkSpeed ~= SOUR_SPEED end
				Humanoid.WalkSpeed = SOUR_SPEED
				task.wait(SOUR_INTERVAL)
				Humanoid.WalkSpeed = DEFAULT_SPEED
			end
		end
	end
	-- Drink
	if Drinks[Name.Value] then
		local Drink = Drinks[Name.Value]
	end
end

MainEvent.OnServerEvent:Connect(Activated)

Any help is appreciated, thank you so much!! If you want any sort of explanation of the code or more information, please ask me.

game.Players.PlayerAdded:Connect(function(player)
	--Everything that needs to run once, when the player first joins
	player.CharacterAdded:Connect(function(character)
		--Everything you want to run each time their character spawns
	end)
end)
2 Likes

Thank you, I didn’t know that was gonna work as I already used Player.CharacterAppearanceLoaded and it didn’t work. And also sometimes the hunger goes under 0 (like -20) after the first respawn, do you know how to fix this?

try to clamp it between your max health and your minimum health (zero)

1 Like

can you show me how to do this and where I would put it? I don’t know how math.clamp works. But I saw some articles on it and it’s exactly what I need

math.clamp() will always return the minimum value if given value goes below and maximum value if your value exceeds it or it returns your value if both conditions are false,

example:

local num = 5
local num2 = -1
local newnum = math.clamp(num,0,4) -- returns 4
local newnum2 = math.clamp(num2, 0, 4) --returns 0
1 Like

thank you so much but I’m not sure where I would put it, do I put it each time I decrease/increase the players hunger (Hunger.Value -= DECREASE_AMOUNT) or do I just put it when I’m declaring the variable hunger

You can have it as

local MinimumHunger = 0
local MaximumHunger = 100

(Hunger.Value -= DECREASE_AMOUNT)
Hunger.Value = math.clamp(Hunger.Value, MinimumHunger, MaximumHunger)
1 Like

Oh okay thank you so much!!!
(edit: sorry another question) If I use math.clamp can I remove the checks like if Hunger.Value >= MIN_HUNGER and not Hunger.Value + DECREASE_AMOUNT >= MIN_HUNGER then @Brehbrehhh213123

1 Like

You can if it is necessary to you, just note that math.clamp will always return what’s described in it’s range

1 Like