Why does my hunger script not work?

i have a script for a hunger bar, and sprint. but for some reason the hunger bar does not go down

local UIS = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local NormalWalkSpeed = 40
local NewWalkSpeed = 70
local sprinting = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character

UIS.InputBegan:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = NewWalkSpeed
		sprinting = true
	end
end)

UIS.InputEnded:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = NormalWalkSpeed
		sprinting = false
	end
end)

local hungval = script.Parent.HGMBackground.Hunger
local bar = script.Parent.HGMBackground.Bar

if sprinting == true then
	while true do
		hungval.Value = hungval.Value - 1 -- i think this is the issue because my hunger bar goes down with the hungval value.
		wait(1)
		if hungval.Value == 0 then
			hungval.Value = 0
			character.Humanoid.Health = character.Humanoid.Health - 1
		end
	end
else
	while true do
		hungval.Value = hungval.Value - 1
		wait(5)
		if hungval.Value == 0 then
			hungval.Value = 0
			character.Humanoid.Health = character.Humanoid.Health - 1
		end
	end
end
while true do
	bar.Size = UDim2.new(hungval.Value / 100, 0, 1, 0)
end

Are you defining at all what the hunger bar does? that just looks like a simple shift sprint script.
Please elaborate further so I can assist you accordingly.

I haven’t tested… but try this code:

while true do
	if sprinting then
		hungval.Value = hungval.Value - 1 -- i think this is the issue because my hunger bar goes down with the hungval value.
		task.wait(1)
		if hungval.Value <= 0 then
			hungval.Value = 0
			character.Humanoid.Health = character.Humanoid.Health - 1
		end
	else
		hungval.Value = hungval.Value - 1
		task.wait(5)
		if hungval.Value <= 0 then
			hungval.Value = 0
			character.Humanoid.Health = character.Humanoid.Health - 1
		end
	end
	bar.Size = UDim2.new(hungval.Value / 100, 0, 1, 0)
end
2 Likes

oh yea my fault accidently deleted some text. it basically goes down to 0 ex:
image
image
, the shift sprint works 100% fine its just that the bar doesnt go down

1 Like

I suggested to change your final block of code… because you have 3 “while true” your code get locked inside it. and never left to change the bar size.

1 Like

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