How come this doesn't work?

I’m trying to make my hunger system decrease faster whenever the player starts sprinting. To do this, I added a BoolValue called IsRunning to the player’s LocalPlayer. The sprinting script enables it. I wrote some basic code in the hunger script that detects if the player’s IsRunning value is true. If it is, then it will change the decrease time value that’s inside of the hunger script to something lower, so the hunger decreases faster. The problem is that it doesn’t seem to work. Help?

Edit: whoops wrong code, fixed now

Code:

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 = 18.1
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

local RunDecrease = Plr:FindFirstChild("IsRunning")

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) do
	if HungerValue.Value - 1 >= 0 then
		HungerValue.Value = HungerValue.Value - 1
	end
	
if RunDecrease.Value == true then
		script.DecreaseValue.Value = 1 -- decreases the time it takes for hunger/thirst to decrease.  
	end	
	
	if HungerValue.Value == 0 then
		repeat wait(1)
			Hum.Health = Hum.Health - 2
		until HungerValue.Value > 0 or Hum.Health <= 0
	end
end

Is this local script? and do you pull any errors from output?

I think it need to be

wait(DecreaseRate.Value)

It’s a local script inside of a GUI. I don’t get any errors.

1 Like

Sorry, I wasn’t clear enough. The code I wrote for reducing the time needed for the hunger to be decreased was

if RunDecrease.Value == true then
		script.DecreaseValue.Value = 1 -- decreases the time it takes for hunger/thirst to decrease.  
	end	

everything else works

So if I understand correctly hunger Value should decrease even if they are not sprinting but it will decrease faster if player sprinting?

That’s exactly what I’m trying to do.

I will try to fix it tmr if you haven’t found solution yet it’s uh almost midnight here

alr so everything worked except this value

so currently it reduce like this

and i fixed the wait and it work like this

is this what you want to fix? or i am misunderstanding something here

Edit: Incase the second video is expected behavior here is the edited code

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 = 18.1
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

local RunDecrease = Plr:FindFirstChild("IsRunning")

--[[if not RunDecrease then --Create IsRunning Boolean if it not exist
	Instance.new("BoolValue", Plr).Name = "IsRunning"
	Plr.IsRunning.Value = true
	RunDecrease = Plr.IsRunning 
end]]

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()
	HG:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0, 0.55,0), "Out", "Linear", .5, true)
end)


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

	if RunDecrease.Value == true then
		script.DecreaseValue.Value = 1 -- decreases the time it takes for hunger/thirst to decrease.  
	end	

	if HungerValue.Value == 0 then
		repeat wait(1)
			Hum.Health = Hum.Health - 2
		until HungerValue.Value > 0 or Hum.Health <= 0
	end
end

Yeah like that, but for some reason, it decreases ALL the GUI, and not just the bar itself.

Like this

Normal:

What happened:

May I see the explorer where the GUI exist?

yeet

image
what about code inside these script something may bind to Hunger Value that decrease value with it (sorry if it sound confusing)