Hunger resets to 100 when I eat food

I’ve followed this tutorial How to create a RPG Hunger System and every time I eat the food my hunger value resets to 100. How do I fix this?
Here’s my code:

HungerScript (in ServerScriptService)

game.Players.PlayerAdded:Connect(function(player)
	local playerWellness = Instance.new("Folder")
	playerWellness.Name = "Wellness"
	playerWellness.Parent = player
	
	local hunger = Instance.new("IntValue")
	hunger.Name = "Hunger"
	hunger.Parent = playerWellness
	hunger.Value = 100
	
	local rep = game:GetService("ReplicatedStorage")
	rep.Events.EatFood.OnServerEvent:Connect(function(player)
		hunger.Value = hunger.Value + 20
		if hunger.Value >= 100 then
			hunger.Value = 100
		end
	end)
end)

ModuleScript (in the Food tool)

local module = {}

local rep = game:GetService("ReplicatedStorage")

function module.EatFood()
	rep.Events.EatFood:FireServer()
end

return module

LocalScript (in the food tool)

local module = require(script.Parent:WaitForChild("ModuleScript"))

local player = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	module.EatFood()
end)

LocalScript (in StarterGui)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

while true do
	if player.Wellness.Hunger.Value == 0 then
		humanoid.Health = humanoid.Health - 1
		wait(1)
	else
		player.Wellness.Hunger.Value = player.Wellness.Hunger.Value - 1
		wait(1)
	end
	print("Hunger is: ".. player.Wellness.Hunger.Value)
end

You’re decreasing the hunger value in a LocalScript (in the while loop). You should decrease your Hunger Value in a Server-Script instead. Changing the Hunger value in a Local Scripts will not replicate to the Server.

1 Like

When I made this tutorial I had nearly no scripting knowledge. As previously stated, I made a mistake and decided to loop decrease hunger on the client instead of on the server. There are two ways you can go about fixing this:

  1. Add a loop on the server to decrease all players’ hunger value:
local Players = game:GetService("Players")

while task.wait(1) do
for _,player in Players:GetPlayers() do
    local character = player.Character or player.CharacterAdded:Wait()
    if player.Wellness.Hunger.Value == 0 then 
        character.Humanoid.Health -= 1 
    else
        player.Wellness.Hunger.Value -= 1
    end
end
  1. Add a loop to the PlayerAdded function:
while task.wait(1) do
    local character = player.Character or player.CharacterAdded:Wait()
    if player.Wellness.Hunger.Value == 0 then 
        character.Humanoid.Health -= 1 
    else
        player.Wellness.Hunger.Value -= 1
    end
end

I definitely don’t recommend using my tutorial, as it is outdated and not well made whatsoever. I will likely create a new tutorial sometime soon. These are just temporary fixes, hopefully they help you out a little bit.

Okay thank you!

thissentencespurposeistogethtebpostto30charactersoicansendit

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.