The code works when the player first spawns in but if the player dies it stops working completely.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local userInputService = game:GetService("UserInputService")
local foodGui = player:WaitForChild("PlayerGui"):WaitForChild("FoodGui")
local slicesFrame = foodGui:WaitForChild("SlicesFrame")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local userInputService = game:GetService("UserInputService")
local foodGui = player:WaitForChild("PlayerGui"):WaitForChild("FoodGui")
local slicesFrame = foodGui:WaitForChild("SlicesFrame")
local maxFood = 100 -- Max food (10 slices)
local sliceSize = 10 -- Food points per slice
local dangerousFood = 30 -- Food threshold for shaking slices
local foodDepletionRateWalking = 0.3 -- Food points depleted per interval while walking
local foodDepletionRateRunning = 0.7 -- Food points depleted per interval while running
local foodDepletionRateJumping = 1 -- Food points depleted per interval while jumping
local foodDepletionInterval = 1 -- Interval in seconds
local healthReplenishRate = 5 -- Health points replenished per food point
local originalWalkSpeed = 16
local sprintWalkSpeed = 32 -- Adjust sprint speed as needed
local depletedWalkSpeed = 8
-- Function to update food bar GUI
local function updateFoodBar(food)
for i = 1, maxFood / sliceSize do
local emptySlice = slicesFrame:FindFirstChild("EmptySlice" .. i)
local fullSlice = emptySlice and emptySlice:FindFirstChild("FullSlice" .. i)
if fullSlice and emptySlice then
local sliceFood = (i - 1) * sliceSize
if food > sliceFood then
local remainingFood = food - sliceFood
local fillAmount = math.clamp(remainingFood / sliceSize, 0, 1)
fullSlice.ImageTransparency = 1 - fillAmount
fullSlice.Visible = true
else
fullSlice.Visible = false
end
else
warn("Missing slice GUI element: EmptySlice" .. i .. " or FullSlice" .. i)
end
end
end
-- Function to deplete food over time
local function depleteFoodOverTime()
while true do
wait(foodDepletionInterval)
local depletionRate = 0
if humanoid.MoveDirection.Magnitude > 0 then
if humanoid:GetState() == Enum.HumanoidStateType.Jumping then
depletionRate = foodDepletionRateJumping
elseif userInputService:IsKeyDown(Enum.KeyCode.LeftShift) or userInputService:IsKeyDown(Enum.KeyCode.RightShift) then
depletionRate = foodDepletionRateRunning
else
depletionRate = foodDepletionRateWalking
end
end
if depletionRate > 0 then
local foodStat = player:FindFirstChild("Food")
if foodStat then
foodStat.Value = math.max(foodStat.Value - depletionRate, 0)
end
end
local foodStat = player:FindFirstChild("Food")
if foodStat then
if foodStat.Value <= 0 then
humanoid.WalkSpeed = depletedWalkSpeed
humanoid.JumpPower = 0
elseif userInputService:IsKeyDown(Enum.KeyCode.LeftShift) or userInputService:IsKeyDown(Enum.KeyCode.RightShift) then
humanoid.WalkSpeed = sprintWalkSpeed
humanoid.JumpPower = 50
else
humanoid.WalkSpeed = originalWalkSpeed
humanoid.JumpPower = 50
end
end
end
end
-- Function to monitor jumping
local function monitorJumping()
local lastJumpState = false
while true do
wait()
local currentJumpState = humanoid:GetState() == Enum.HumanoidStateType.Jumping
if currentJumpState and not lastJumpState then
-- Just started jumping
local foodStat = player:FindFirstChild("Food")
if foodStat then
foodStat.Value = math.max(foodStat.Value - foodDepletionRateJumping, 0)
end
end
lastJumpState = currentJumpState
end
end
-- Function to replenish health using food
local function replenishHealth()
while true do
wait(1) -- Check every second
local foodStat = player:FindFirstChild("Food")
if foodStat and foodStat.Value > 0 and humanoid.Health < humanoid.MaxHealth then
local healthToReplenish = math.min(foodStat.Value, healthReplenishRate)
foodStat.Value = foodStat.Value - healthToReplenish
humanoid.Health = math.min(humanoid.Health + healthToReplenish, humanoid.MaxHealth)
updateFoodBar(foodStat.Value)
end
end
end
-- Connect to character added event
local function onCharacterAdded(character)
humanoid = character:WaitForChild("Humanoid")
-- Initialize food stat
if not player:FindFirstChild("Food") then
local foodStat = Instance.new("NumberValue")
foodStat.Name = "Food"
foodStat.Value = maxFood
foodStat.Parent = player
foodStat.Changed:Connect(function(value)
updateFoodBar(value)
end)
else
player.Food.Value = maxFood -- Reset hunger on respawn
end
-- Update food GUI
updateFoodBar(player:FindFirstChild("Food").Value)
-- Start depleting food over time
spawn(depleteFoodOverTime)
-- Start monitoring jumping
spawn(monitorJumping)
-- Start replenishing health using food
spawn(replenishHealth)
end
player.CharacterAdded:Connect(onCharacterAdded)
-- Check if character already exists
if player.Character then
onCharacterAdded(player.Character)
end
-- Function to handle respawn
local function onPlayerRespawn()
local character = player.Character or player.CharacterAdded:Wait()
onCharacterAdded(character)
end
player.CharacterAdded:Connect(onPlayerRespawn)
I think that if it’s a local script the character should be always the same bc it’s ran on players computer, try disabling this function maybe that will do. Or i’m just dumb
Do you have ResetOnSpawn properties on FoodGui set to true? I have tried your code and it seem to working fine (not sure if stacking depletion event was on purpose or not)
everything in video is the same in your code except depletion rate/replaced FullSlice ImageTransparency to FullSlice BackgroundTransparency instead.
I assuming that the script is currently in startercharacterscript if that it the case try rebinding foodStat.Changed event in food number instance
-- Connect to character added event
local function onCharacterAdded(character)
humanoid = character:WaitForChild("Humanoid")
-- Initialize food stat
if not player:FindFirstChild("Food") then
local foodStat = Instance.new("NumberValue")
foodStat.Name = "Food"
foodStat.Value = maxFood
foodStat.Parent = player
foodStat.Changed:Connect(function(value)
updateFoodBar(value)
end)
else
local foodStat = player.Food
foodStat.Changed:Connect(function(value)
updateFoodBar(value)
end)
player.Food.Value = maxFood -- Reset hunger on respawn
end
-- Update food GUI
updateFoodBar(player:FindFirstChild("Food").Value)
-- Start depleting food over time
spawn(depleteFoodOverTime)
-- Start monitoring jumping
spawn(monitorJumping)
-- Start replenishing health using food
spawn(replenishHealth)
end