Problem with Running script

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)

is the script local script if so
Where did u put it?

Obvs local im not that dumb, iv been scripting for atleast 1 year,Starter player script

Make it so it check if humanoid died

Where,when,why, i want to understand not just copy paste

Is this in StarterPlayerScripts? If then maybe try to move it StarterCharacterScripts since when character respawns it clones the script inside character model so code runs from starts again.

1 Like

It actually fixed, now it work properly, seems like the script just needed to reset each time the player died,even tho i prefer having scripts in player rather then character since its cleaner but still thanks.

you only load the animation one time, so when the player dies the humanoid doesn’t have the animation loaded

you should put after the “track” variable declaration a humanoid.Died connection and load the animation again.

Also, about the infinite stamina drain, the way your stamina is set up right now, if you die before your stamina is 0, it will keep draining stamina. a way to fix this would be to just break after both

1 Like