Hello!
I am currently trying to make a Stamina System for the Player, so that it won't be able to run forever. This works, however, if the player holds down shift without moving, the stamina bar goes down. I thought it may have been an issue caused by the UserInputService as it states that as soon as the key is held down to use stamina, however I am quite unsure and i am also unsure how to fix it.
Here is my script:
local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')
local Player = game:GetService("Players").LocalPlayer
local NormalWalkSpeed = 16
local NewWalkSpeed = 20
local Power = 10
local IsSprinting = false
repeat wait() until Player.Character
local Character = Player.Character
UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
if GameProcessedEvent then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = NewWalkSpeed
IsSprinting = true
while Power > 0 and IsSprinting do
Power = Power - 0.03
Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
wait()
if Power <= 0 then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end)
UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
if GameProcessedEvent then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
IsSprinting = false
while Power < 10 and not IsSprinting do
Power = Power + 0.03
Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
wait()
if Power <= 0 then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end)
If I could get some help with this, it would be great. Thanks!

1 comment
This worked very well. Thanks!