Slow player script

Hey developers, I made a watched a tutorial on how to make a stamina system that makes you faster when you hold down shift for a temporary few seconds and after you un hold shift you could sprint again also I have a system that makes the player walkspeed slow when you have 30/under 30 health. I found a problem with the slow player walkspeed when under 30 health though, So whenever I have 30/under 30 health I would walk slow but I couldn’t sprint.
Here’s the stamina script:

local player = game.Players.LocalPlayer
local character = player.character

local UserInputService = game:GetService(“UserInputService”)

local stamina = 100
local running = false

stamina = math.clamp(stamina, 0,100)

UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
running = true
character.Humanoid.WalkSpeed = 20
while stamina > 0 and running do
stamina = stamina - 1
script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), “Out”, “Linear”, 0)
wait()
if stamina == 0 then
character.Humanoid.WalkSpeed = 16
end
end
end
end)

UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
running = false
character.Humanoid.WalkSpeed = 16
while stamina < 100 and not running do
stamina = stamina + 1
script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), “Out”, “Linear”, 0)
wait()
if stamina == 0 then
character.Humanoid.WalkSpeed = 16
end
end
end
end)

And here’s the slow player walk speed script:

local humanoid = script.Parent:FindFirstChild(“Humanoid”)

while true do – loops bottom code
wait ()
if humanoid.Health <= 30 then – If humanoid health is 30/under change walkspeed
humanoid.WalkSpeed = 10 – Changes walkspeed to 10
elseif
humanoid.Health >= 30 then – If humanoid health is over 30 change walkspeed
humanoid.WalkSpeed = 16 – Changes walkspeed to default
end
end

Thank!

1 Like

Well this happens because both your scripts keep changing between 10 and 20 constantly. What you could do is put all the code in one script, run every while loop on a new thread and only set walkspeed to 10 when not running