How come this breaks everything?

I have this hunger script inside of a GUI. I wanted it to disable the sprinting script inside of another GUI so that you are too hungry to run. Great that worked. However, since it used a repeat until, I couldn’t figure out a way to make it enable the script again. So I tried

	if HungerValue.Value > 0 then
		repeat wait(0.5)
		Plr.PlayerGui.Stamina.Handler.Disabled = false
		until HungerValue == 0

That did the trick, until it didn’t. For some reason, the script just broke. Help?

local HG = script.Parent
local Text = HG:WaitForChild("Calories"):WaitForChild("Text")
local Plr = game.Players.LocalPlayer

repeat wait() until Plr.Character

local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxHunger = 19
local DecreaseRate = script.DecreaseValue -- the lower the number, the faster it decreases.  the higher the number, the longer it will take before decreasing
local HungerValue

if Plr:FindFirstChild("HungerVal") then
	HungerValue = Plr.HungerVal
	HungerValue.Value = MaxHunger
else
	Instance.new("IntValue", Plr).Name = "HungerVal"
	Plr.HungerVal.Value = MaxHunger
	HungerValue = Plr.HungerVal 
end

HungerValue.Changed:connect(function()
	Text:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0, 0.55,0), "Out", "Linear", .5, true)
end)

while wait(DecreaseRate.Value) do
	if HungerValue.Value - 1 >= 0 then
		HungerValue.Value = HungerValue.Value - 1
	end

	if HungerValue.Value == 0 then
		Plr.Character.UpperTorso.StomachGrowling:Play()
		repeat wait(1) 
			Hum.Health = Hum.Health - 2
			Plr.PlayerGui.Stamina.Handler.Disabled = true	
		until HungerValue.Value > 0 or Hum.Health <= 0
		
		if HungerValue.Value > 0 then
		repeat wait(0.5)
		Plr.PlayerGui.Stamina.Handler.Disabled = false
		until HungerValue == 0
			
		end
	end
end

I’d suggest instead of disabling the script, you make a BoolValue. For example a BoolValue named “Hungry”. Then in your hunger script, when they are hungry/not hungry, change the BoolValue. Then in your sprint script, only sprint when they aren’t hungry and stop sprinting when they become hungry by binding the BoolValue’s .Changed event.

The last HungerValue needs a “.Value”

1 Like

I added that, but still nothing happened

Can you show us the output for errors? If the errors are not there, try printing for debug.

There aren’t any errors in output. I just tried printing, and I didn’t get the print("Decreased bar") that I wrote down for when the player’s bar decreases, after the player has starved and stamina disabled, and then ate something.

local HG = script.Parent
local Text = HG:WaitForChild("Calories"):WaitForChild("Text")
local Plr = game.Players.LocalPlayer

repeat wait() until Plr.Character

local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxHunger = 19
local DecreaseRate = script.DecreaseValue -- the lower the number, the faster it decreases.  the higher the number, the longer it will take before decreasing
local HungerValue

if Plr:FindFirstChild("HungerVal") then
	HungerValue = Plr.HungerVal
	HungerValue.Value = MaxHunger
	print("Setup for values complete")
else
	Instance.new("IntValue", Plr).Name = "HungerVal"
	Plr.HungerVal.Value = MaxHunger
	HungerValue = Plr.HungerVal 
	print("Created new value")
end

HungerValue.Changed:connect(function()
	Text:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0, 0.55,0), "Out", "Linear", .5, true)
end)

while wait(DecreaseRate.Value) do
	if HungerValue.Value - 1 >= 0 then
		HungerValue.Value = HungerValue.Value - 1
		print("Decreased bar")
	end

	if HungerValue.Value == 0 then
		Plr.Character.UpperTorso.StomachGrowling:Play()
		Plr.PlayerGui.Stamina.Handler.Disabled = true	
		print("Player is now hungry and stamina disabled")
		repeat wait(1) 
		Hum.Health = Hum.Health - 2
		until HungerValue.Value > 0 or Hum.Health <= 0
		
		if HungerValue.Value > 0 then
		print("Stamina Enabled")	
		repeat wait(0.5)
		Plr.PlayerGui.Stamina.Handler.Disabled = false
		until HungerValue.Value == 0
			
		end
	end
end