I’ve been trying to make a stamina system that starts regenerating when the player stops moving. Something that is stopping me from doing this is that the script is not letting the sprinting stop even after releasing the sprint key. Below is a video and the code responsible for this.
UIS.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
while UIS:IsKeyDown(Enum.KeyCode.LeftShift) or UIS:IsKeyDown(Enum.KeyCode.RightShift) do
task.wait()
Sprint = true
print("sprint enabled")
Stamina -= 1
Stamina = math.clamp(Stamina,0,MaxStamina)
end
end
end)
UIS.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
Running = Running
Sprint = false
print("sprint disabled")
while Running ~= true do
task.wait()
Stamina += 1
end
end
end)
while game:GetService("RunService").RenderStepped:Wait() do
if Sprint == true then
Humanoid.WalkSpeed = 20
else
Humanoid.WalkSpeed = 16
end
end