Sprinting Animation working weirdly

My animation is working really weirdly.
I am clueless to what’s going on.
I can explain it as the sprinting animation playing in tandem with the default walking animation.
Here’s a video:
robloxapp-20240627-2115569.wmv (1.7 MB)

And, here’s the script.

-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- Variables
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character and Character:WaitForChild("Humanoid")

local Camera = workspace.CurrentCamera

-- Constants
local CommonVariables = {}
CommonVariables.SprintingKeyCode = Enum.KeyCode.LeftShift
CommonVariables.DefaultHumanoidWalkSpeed = 12
CommonVariables.DefaultCameraFieldOfView = 70 -- // OPTIONAL
CommonVariables.CurrentWalkSpeed = CommonVariables.DefaultHumanoidWalkSpeed
CommonVariables.RunningMultipiler = 2.5
CommonVariables.SprintingAlpha = 0.1

local SprintAnim = Humanoid.Animator:LoadAnimation(script:WaitForChild("PlayerSprint"))
local Sprinting = false

-- Functions
local function UpdatedLocalVariables(newCharacter)
	Humanoid = newCharacter:WaitForChild("Humanoid")
	Character = newCharacter
end

local function Lerp(Start, End, Alpha) : number
	return Start + (End - Start) * Alpha
end

local function IsActorDied()
	if Humanoid then
		if Humanoid.Health <= 0 then
			return true
		else
			return false
		end
	else
		warn(("[%s]: Humanoid not found!"):format(script.Name))
		return true
	end
end

local function RenderStepped(Deltatime)
	local ActorDied = IsActorDied()

	if not ActorDied and Character then
		if UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) and Humanoid.MoveDirection.Magnitude > 0 then
			local Sprinting = true
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed * CommonVariables.RunningMultipiler,
				
				
				CommonVariables.SprintingAlpha
				
			)
			SprintAnim:Play()
		else
			local Sprinting = false
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed,

				CommonVariables.SprintingAlpha
			)
			SprintAnim:Stop()
		end
	else
		local Sprinting = false
		CommonVariables.CurrentWalkSpeed = Lerp(
			CommonVariables.CurrentWalkSpeed,
			CommonVariables.DefaultHumanoidWalkSpeed,

			CommonVariables.SprintingAlpha
		)
		SprintAnim:Stop()
	end

-- Other Stuff
	Camera.FieldOfView = Lerp(
		Camera.FieldOfView,
		(UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) and CommonVariables.DefaultCameraFieldOfView * 1.25 or CommonVariables.DefaultCameraFieldOfView),
		0.05
	)


	Humanoid.WalkSpeed = CommonVariables.CurrentWalkSpeed
end


RunService.RenderStepped:Connect(RenderStepped)
Player.CharacterAdded:Connect(UpdatedLocalVariables)

edit: the sprint animation’s priority is higher too

1 Like

What priority is the running animation? If it’s Movement then it would interfere with the walking animation and should be set to a higher priority like Action if you want it to override.
An alternative solution would be to just get the Animate script in the character during testing and place it into StarterCharacterScripts.
From there you can edit it and add an additional state for sprinting. Here’s some images of the parts you would need to edit, as well as their position in the script.

Hope this helps!

Sadly, the second method didn’t work…
But, for extra information, the sprinting animation is set to action.

Did it not work due to error messages or did it just not override the walk animation?
If possible, could I have a look at your code?
It’s a bit late right now, so I might check it later.

I just didn’t override the walking animation. It still played in tandem with it.
I copied the sprint script and removed the animation playing parts then disabled the original one. When I tested it, the animation sprinting animation did not override the walking animation.

Here’s the important parts from the Animate script.

local animNames = { 
	idle = 	{	
				{ id = "http://www.roblox.com/asset/?id=180435571", weight = 9 },
				{ id = "http://www.roblox.com/asset/?id=180435792", weight = 1 }
			},
	walk = 	{ 	
				{ id = "http://www.roblox.com/asset/?id=180426354", weight = 10 } 
	}, 
	sprint = 	{ 	
		{ id = "http://www.roblox.com/asset/?id=18241817709", weight = 10 } 
	}, 
	run = 	{
				{ id = "run.xml", weight = 10 } 
			}, 
	jump = 	{
				{ id = "http://www.roblox.com/asset/?id=125750702", weight = 10 } 
			}, 
	fall = 	{
				{ id = "http://www.roblox.com/asset/?id=180436148", weight = 10 } 
			}, 
	climb = {
				{ id = "http://www.roblox.com/asset/?id=180436334", weight = 10 } 
			}, 
	sit = 	{
				{ id = "http://www.roblox.com/asset/?id=178130996", weight = 10 } 
			},	
	toolnone = {
				{ id = "http://www.roblox.com/asset/?id=182393478", weight = 10 } 
			},
	toolslash = {
				{ id = "http://www.roblox.com/asset/?id=129967390", weight = 10 } 
--				{ id = "slash.xml", weight = 10 } 
			},
	toollunge = {
				{ id = "http://www.roblox.com/asset/?id=129967478", weight = 10 } 
			},
	wave = {
				{ id = "http://www.roblox.com/asset/?id=128777973", weight = 10 } 
			},
	point = {
				{ id = "http://www.roblox.com/asset/?id=128853357", weight = 10 } 
			},
	dance1 = {
				{ id = "http://www.roblox.com/asset/?id=182435998", weight = 10 }, 
				{ id = "http://www.roblox.com/asset/?id=182491037", weight = 10 }, 
				{ id = "http://www.roblox.com/asset/?id=182491065", weight = 10 } 
			},
	dance2 = {
				{ id = "http://www.roblox.com/asset/?id=182436842", weight = 10 }, 
				{ id = "http://www.roblox.com/asset/?id=182491248", weight = 10 }, 
				{ id = "http://www.roblox.com/asset/?id=182491277", weight = 10 } 
			},
	dance3 = {
				{ id = "http://www.roblox.com/asset/?id=182436935", weight = 10 }, 
				{ id = "http://www.roblox.com/asset/?id=182491368", weight = 10 }, 
				{ id = "http://www.roblox.com/asset/?id=182491423", weight = 10 } 
			},
	laugh = {
				{ id = "http://www.roblox.com/asset/?id=129423131", weight = 10 } 
			},
	cheer = {
				{ id = "http://www.roblox.com/asset/?id=129423030", weight = 10 } 
			},
}
local sprintingspeed = 24
function onRunning(speed)
	speed /= getRigScale()
	
	if speed > 0.01 then
		if speed > sprintingspeed then
			playAnimation("walk", 0.1, Humanoid)
			if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=18241817709" then
				setAnimationSpeed(speed / 14.5)
				pose = "Running"
			end
		else
		playAnimation("walk", 0.1, Humanoid)
		if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=180426354" then
			setAnimationSpeed(speed / 14.5)
			pose = "Running"
			end	
		end
	else
		if emoteNames[currentAnim] == nil then
			playAnimation("idle", 0.1, Humanoid)
		pose = "Standing"
		   end
	    end
	end
	if (pose == "FreeFall" and jumpAnimTime <= 0) then
		playAnimation("fall", fallTransitionTime, Humanoid)
	elseif (pose == "Seated") then
		playAnimation("sit", 0.5, Humanoid)
		return
	elseif (pose == "Running") then
		playAnimation("walk", 0.1, Humanoid)
	elseif (pose == "Sprinting") then
		playAnimation("sprint", 0.1, Humanoid)
	elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
--		print("Wha " .. pose)
		stopAllAnimations()
		amplitude = 0.1
		frequency = 1
		setAngles = true
	end

In the second block of code, you didn’t use the correct values for the playAnimation function and the pose.

In the first block of code, you’re using the value “sprint” to represent the sprinting animations, whereas in the second block it’s “walk”, the same animation as the walking animation. Similarly you’re setting the pose to “Running” which is used to replay the walking animation in the third block of code. This means that you aren’t playing the sprinting animation at all, rather just playing the walking animation instead.

To fix this, when you check if the speed exceeds the sprint speed set the pose to “Sprinting” (the same as the one in the third code block) and the played animation to “sprint” (the same as the one in the first code block)

It works! Although, the animation is playing faster than its supposed to.
Edit: I managed to fix the issue rather easily. Thanks for the help!

That could be due to setAnimationSpeed, maybe just try setting it to 1, however I haven’t tested it before so it may not work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.