Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Sprint Animation keeps playing after I stop moving?

Hey,

I have an issue with my sprint script, essentially when i stand still and hold left shift and then start moving and then i stop moving but keep holding shift, the animation still plays

my code :

local p = game.Players.LocalPlayer
local c = p.Character
local h = c:WaitForChild('Humanoid')
local u = game:GetService('UserInputService')
local held = false
local stepped = game:GetService('RunService').Stepped
local sprintanim = Instance.new('Animation', script)
sprintanim.AnimationId = 'rbxassetid://6496505792'
local a = h:WaitForChild("Animator")
local strack = a:LoadAnimation(sprintanim)

local sprinting = Instance.new("BoolValue", h)
sprinting.Name = 'Sprinting'

function sp(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		held = true
		if held == true and h.MoveDirection.magnitude>=0 then
			h.WalkSpeed = 30 
			strack:Play()
			sprinting = true
			if held == true and h.MoveDirection.magnitude == 0 and strack.IsPlaying then
				strack:Stop()
			end
			if held == true and h.MoveDirection.magnitude == 0 or c.HumanoidRootPart.Velocity == 0 then
				if strack.IsPlaying then
					strack:Stop()
				end
				repeat wait() until
				h.MoveDirection.magnitude>0 
				strack:Play()
			end
		end
	end
end

function ps(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		held = false
		sprinting = false
		if held == false and sprinting == false then
			h.WalkSpeed = 16
			strack:Stop()
		end
	end
end

u.InputBegan:connect(sp)
u.InputEnded:connect(ps)

2 comments

0

Are there any errors in the output? (You can open the output by going to: View>Output

0

No, everything works fine except what i said in the post

1 answer

✓ Accepted answer

The reason your code doesn't work is because function only runs once. This means that it doesn't check constantly. To fix, add a while loop checking inside the function. Here is the revised code.

If the code doesn't work, check for these things as this is what I did:

Make the animation looped, Make the animation priority movement, and Make sure no other animations are playing

local p = game.Players.LocalPlayer
local c = p.Character
local h = c:WaitForChild('Humanoid')
local u = game:GetService('UserInputService')
local held = false
local stepped = game:GetService('RunService').Stepped
local sprintanim = Instance.new('Animation', script)
sprintanim.AnimationId = 'rbxassetid://6502002061'
local a = h:WaitForChild("Animator")
local strack = a:LoadAnimation(sprintanim)

local sprinting = Instance.new("BoolValue", h)
sprinting.Name = 'Sprinting'

function sp(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		held = true
		if held == true and h.MoveDirection.magnitude>=0 then
			h.WalkSpeed = 30 
			strack:Play()
			sprinting = true
			if held == true and h.MoveDirection.magnitude == 0 and strack.IsPlaying then
				strack:Stop()
			end
			if held == true and h.MoveDirection.magnitude == 0 or c.HumanoidRootPart.Velocity == 0 then
				if strack.IsPlaying then
					strack:Stop()
				end
				repeat wait() until
				h.MoveDirection.magnitude>0 
				strack:Play()
			end
			
		end
		while held do 
			if h.MoveDirection.magnitude == 0 then
				strack:Stop()
			end
			if h.MoveDirection.magnitude ~= 0 and strack.isPlaying == false then
				strack:Play()
			end
			wait()
		end
	end
end

function ps(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		held = false
		sprinting = false
		if held == false and sprinting == false then
			h.WalkSpeed = 16
			strack:Stop()
		end
	end
end


u.InputBegan:connect(sp)
u.InputEnded:connect(ps)


1 comment

0

Thank you! I actually had a gut feeling this would need a while loop but i still went for the repeat until, you know im still learning

This discussion is preserved as it was. New answers, comments, and votes are closed.