Stamina Bar Help

I am trying to fix my stamina bar. When a player spams the shift key it makes the values go down faster

I have tryed adding checks to make it only allow the script to run one time but I am probably making it in the wrong spot.

Look at the bottom left blue bar.

What happens when you spam shift:

What happens when you hold shift:

function UpdateSprintbar()
	local Sprintbar = script.Parent.Frame.Data.Stamina.StaminaBar
	local Sprint = math.clamp(LocalPlayer:GetAttribute("Sprint") / LocalPlayer:GetAttribute("MaxSprint"), 0, 1)
	Sprintbar.Size = UDim2.fromScale(Sprint, 1)
	Sprintbar.Parent.TextLabel.Text = LocalPlayer:GetAttribute("Sprint") .. " / " .. LocalPlayer:GetAttribute("MaxSprint")
end

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
	if gameProcessed then return end
	if inputObject.KeyCode == LocalPlayer:GetAttribute("Sprintkey") and not running then
		running = true
		while UserInputService:IsKeyDown(LocalPlayer:GetAttribute("Sprintkey")) and LocalPlayer:GetAttribute("Sprint") > 0 and running do
			LocalPlayer:SetAttribute("Sprint",LocalPlayer:GetAttribute("Sprint") - 1)
			UpdateSprintbar()
			if running then
				task.wait(1)
				end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == LocalPlayer:GetAttribute("Sprintkey") then
			running = false
			warn("not running")
			while not running and LocalPlayer:GetAttribute("Sprint") ~= LocalPlayer:GetAttribute("MaxSprint") do

				LocalPlayer:SetAttribute("Sprint",LocalPlayer:GetAttribute("Sprint") + 1)
				UpdateSprintbar()
				task.wait(1)
			end
		end
	end
end)
1 Like

instead of having the inputEnded function set running to false, put that to after the while loop in the input began function, and add a wait 1 seconds before it.

You could also add a debounce

1 Like

^^^^ Let us know if this works!

2 Likes

Thanks it worked. I did not add the wait.

1 Like

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