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

sprint animation stop playing when i restart walking ,how can i fix?

so i made this script and when i stop walking and then restart it play walk anim ,not sprint anim

local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local sprintAnim = humanoid:LoadAnimation(script:WaitForChild("SprintAnim"))
local isSprinting = false

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		sprintAnim:AdjustSpeed(1)
	else
		sprintAnim:Stop()
	end
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if not isSprinting then
			isSprinting = true
			sprintAnim:Play()
			sprintAnim:AdjustSpeed(0)
			humanoid.WalkSpeed = 26
			humanoid.JumpPower = 50
		else
			sprintAnim:Stop()
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = 50
			isSprinting = false	
		end	
	end
end)

2 comments

0

u need to put a local script in startercharacterscript with a animation in it to test it

2 answers

✓ Accepted answer

you need to play the animation whenever the character starts running, not just when the player presses shift

humanoid.Running:Connect(function(speed)
    if speed > 0 then
		if isSprinting and not sprintAnim.IsPlaying then
			sprintAnim:Play()
		end
        sprintAnim:AdjustSpeed(speed/26) -- pretty sure you'd want the animation's playback speed to change based on how fast the player is actually walking 
    else
        sprintAnim:Stop()
    end
end)

also, the player could press shift when they're standing still, so you'd need to check for that to make sure the sprinting animation doesn't play while they're not moving

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if not isSprinting then
            isSprinting = true
			if humanoid.MoveDirection.Magnitude > 0 then -- just checking the player's MoveDirection is the easiest way to tell if they're not pressing any movement keys
				sprintAnim:Play()
				sprintAnim:AdjustSpeed(0)
			end
            humanoid.WalkSpeed = 26
            humanoid.JumpPower = 50
        else
            sprintAnim:Stop()
            humanoid.WalkSpeed = 16
            humanoid.JumpPower = 50
            isSprinting = false 
        end 
    end
end)

2 comments

0

thanks u helped me

0

i just added a credit of you

u need to put a local script in startercharacterscript with a animation in it to test it

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