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