Sprinting sometimes doesn't work on respawn

I’m facing a problem where sometimes when a player spawns, their sprinting and walking animations are changed, and the sprinting doesn’t work. The only way they could fix this is by respawning again, and hoping it doesn’t happen.

Here is the script that is currently inside of StarterCharacterScripts:

local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")

local customWalkAnimId = "rbxassetid://"
local customRunAnimId = "rbxassetid://"  

local isSprinting = false

local function setupCharacter(character)
	local Humanoid = character:WaitForChild("Humanoid")
	local AnimateScript = character:WaitForChild("Animate")

	AnimateScript.walk.WalkAnim.AnimationId = customWalkAnimId

	local function handleSprint(actionName, inputState)
		if actionName == "Sprint" then
			if inputState == Enum.UserInputState.Begin then
				isSprinting = true
				Humanoid.WalkSpeed = 14

				if customRunAnimId ~= "" then
					AnimateScript.run.RunAnim.AnimationId = customRunAnimId
				else
					AnimateScript.run.RunAnim.AnimationId = ""
				end
			elseif inputState == Enum.UserInputState.End then
				isSprinting = false
				Humanoid.WalkSpeed = 6

				AnimateScript.walk.WalkAnim.AnimationId = customWalkAnimId
			end
		end
	end

	Humanoid.Running:Connect(function(speed)
		if isSprinting then
			if customRunAnimId == "" then
				AnimateScript.walk.WalkAnim.AnimationId = "" 
			end
		else
			AnimateScript.walk.WalkAnim.AnimationId = customWalkAnimId
		end
	end)
	ContextActionService:BindAction("Sprint", handleSprint, true, Enum.KeyCode.LeftShift)
end

local Character = Player.Character or Player.CharacterAdded:Wait()
setupCharacter(Character)

Player.CharacterAdded:Connect(function(newCharacter)
	setupCharacter(newCharacter)
end)

Here is the footage of how it’s supposed to look vs how it looks when the problem occcurs:

Normal - Working Animations
The Problem - Broken Animations

2 Likes

It could because of the binded action. When the player dies, Humanoid.Died:Connect(), unbind the event so the script can reuse it. I’m guessing this is a local script in the character, so if the problem is just the handleSprint not being called, it could be that.

2 Likes

I think I understand what you’re trying to say but

If this script is inside StarterCharacterScripts, it doesn’t need any connections. When your character gets destroyed, the script will be removed as well.

See if this matches what you mean:

Just so you know, I used PascalCase instead of camelCase, based on your example.

--> Services <--

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

--> Variables <--

local Player = Players.LocalPlayer

local CustomWalkAnimId = "rbxassetid://"
local CustomRunAnimId = "rbxassetid://"

local Humanoid = script.Parent:WaitForChild("Humanoid")
local AnimateScript = script.Parent:WaitForChild("Animate")

local IsSprinting = false

--> Functions <--

local function HandleSprint(ActionName, InputState)
	if ActionName == "Sprint" then
		if InputState == Enum.UserInputState.Begin then
			IsSprinting = true
			Humanoid.WalkSpeed = 14
			AnimateScript.run.RunAnim.AnimationId = CustomRunAnimId
		elseif InputState == Enum.UserInputState.End then
			IsSprinting = false
			Humanoid.WalkSpeed = 6
			AnimateScript.walk.WalkAnim.AnimationId = CustomWalkAnimId
		end
	end
end

--> Main <--

Humanoid.WalkSpeed = 6

AnimateScript.walk.WalkAnim.AnimationId = CustomWalkAnimId
AnimateScript.run.RunAnim.AnimationId = CustomRunAnimId

ContextActionService:BindAction("Sprint", HandleSprint, true, Enum.KeyCode.LeftShift)
2 Likes

The script is in StarterCharacterScripts, so you shouldn’t make a connection to player added. Instead, just get the character using script.Parent.

The problem might be that because there are multiple connections going, the effects are being overlayed on each other. Edit: I believe the specific problem is that the old connection calls CAS:BindAction, which sometimes overwrites the binding for the current character (they run on the same event so the order of bindings is basically random).

Here is that change:

local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")

local customWalkAnimId = "rbxassetid://"
local customRunAnimId = "rbxassetid://"  

local isSprinting = false

local function setupCharacter(character)
	local Humanoid = character:WaitForChild("Humanoid")
	local AnimateScript = character:WaitForChild("Animate")

	AnimateScript.walk.WalkAnim.AnimationId = customWalkAnimId

	local function handleSprint(actionName, inputState)
		if actionName == "Sprint" then
			if inputState == Enum.UserInputState.Begin then
				isSprinting = true
				Humanoid.WalkSpeed = 14

				if customRunAnimId ~= "" then
					AnimateScript.run.RunAnim.AnimationId = customRunAnimId
				else
					AnimateScript.run.RunAnim.AnimationId = ""
				end
			elseif inputState == Enum.UserInputState.End then
				isSprinting = false
				Humanoid.WalkSpeed = 6

				AnimateScript.walk.WalkAnim.AnimationId = customWalkAnimId
			end
		end
	end

	Humanoid.Running:Connect(function(speed)
		if isSprinting then
			if customRunAnimId == "" then
				AnimateScript.walk.WalkAnim.AnimationId = "" 
			end
		else
			AnimateScript.walk.WalkAnim.AnimationId = customWalkAnimId
		end
	end)
	ContextActionService:BindAction("Sprint", handleSprint, true, Enum.KeyCode.LeftShift)
end

local Character = script.Parent
setupCharacter(newCharacter)

Another potential problem, specifically if it’s only the animation that’s not working: I believe for the animations to work properly, you should have the Animation instance in AnimateScript.run be swapped out. This forces the animation script to swap the animation, where as if you just change the AnimationId the Animate script doesn’t detect this and only changes the animation if the character stops running then starts again. This might be fine though if you update the animation before the character starts running because of you’re using the Running event.

So instead of setting the AnimationId you would swap the Animation out for a new one with the Id you want then when you’re done swap the original Animation back as a child of Animate.run/Animate.walk.

3 Likes