So im having a problem with this sprint script that also drain stamina and play a run animation.
No output errors
at first when joining the game it work fine but when you die / reset, if you can’t sprint anymore, the animation don’t play and the stamina drain forever.
If its nedeed i can provide a video about the bug
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local Lead = Player:WaitForChild("Lead")
local Stamina = Lead:WaitForChild("Stamina")
local LastTapped = false
local Running = false
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://ID"
local track = Humanoid:LoadAnimation(Anim)
local VectorZero = Vector3.new(0, 0, 0)
function Animation(Effect)
if Effect == "StartRun" then
track:Play()
track:AdjustSpeed(Humanoid.WalkSpeed/16)
else
track:Stop()
end
end
Humanoid.StateChanged:Connect(function(old,new)
if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
track:Stop()
elseif new == Enum.HumanoidStateType.Landed then
if Running == true then
Animation("StartRun")
end
end
end)
UIS.InputBegan:Connect(function(Key, gameProcessed)
if not gameProcessed then
if not LastTapped then
if Key.KeyCode == Enum.KeyCode.W then
LastTapped = true
wait(.25)
LastTapped = false
end
else
if Running == false then
Running = true
Humanoid.WalkSpeed = Humanoid.WalkSpeed + 22
print(Humanoid.WalkSpeed)
Animation("StartRun")
spawn(StaminaD)
end
end
end
end)
local function Moved()
if Humanoid.MoveDirection == VectorZero then
if Running == true then
Running = false
Humanoid.WalkSpeed = Humanoid.WalkSpeed -22
Animation("Stop")
spawn(Regen)
end
end
end
function StaminaD()
while Running do
if Stamina.Value <= 0 then
Running = false
Humanoid.WalkSpeed = Humanoid.WalkSpeed -22
Stamina.Value = 0
Animation("Stop")
spawn(Regen)
break
end
Stamina.Value = Stamina.Value - 10
wait(1)
end
end
function Regen()
while not Running do
if Stamina.Value >= 1000 then
Stamina.Value = 1000
break
end
Stamina.Value = Stamina.Value + 10
wait(1)
end
end
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(Moved)