How to transition different animations while moving

i’m working on a test game about freerunning, and i’ve added acceleration to it to make it feel more realistic

problem is though, there can only be one run animation in the default roblox animate

so i’m thinking i’ll make the player transition through a walk animation to a jog animation to a full run animation by checking their walkspeed

so i removed the part in the animate script where it plays the run animation and made a new script in startercharacterscripts that goes like this:


local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local walk = hum:LoadAnimation(script.walk)
local jog = hum:LoadAnimation(script.jog)
local run = hum:LoadAnimation(script.run)

hum.Running:Connect(function(walkspeed)
	if walkspeed < 20 and hum.MoveDirection.Magnitude > 0 then
		walk:Play()
	elseif walkspeed > 20 then
		run:Play()
	end
end)

(i didn’t include the jog just yet, i’m planning to add it once the animation script works)

now it sort of works, but the problem is it constantly updates when the player is running making the animations constantly start playing, and when we’re on full acceleration speed the run looks weird

since i’m kinda stupid i have no idea how to solve this problem, so any help would be appreciated

keep track of what state was used last, and if its the same state as current, then don’t play the animation.

Also if you don’t need to even modify the default animation script, as long as in your custom animation player, you are setting the priority to higher than ‘core’

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local walk = hum:LoadAnimation(script.walk)
local jog = hum:LoadAnimation(script.jog)
local run = hum:LoadAnimation(script.run)
local lastState = ""

hum.Running:Connect(function(walkspeed)
	if walkspeed < 20 and hum.MoveDirection.Magnitude > 0 then
		if lastState ~= "walk" then walk:Play() end
		lastState = "walk"
	elseif walkspeed > 20 then
		if lastState ~= "run" then run:Play() end
		lastState = "run"
	end
end)

hey, this works pretty alright but the animation doesn’t stop if i stop moving or jump etc, how could i stop that from happening

Try this

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local walk = hum:LoadAnimation(script.walk)
local jog = hum:LoadAnimation(script.jog)
local run = hum:LoadAnimation(script.run)

local lastState = ""

while char and char.Parent and hum and hum.Parent and hum.Health > 0 do
	if hum:GetState() == Enum.HumanoidStateType.Running then
		local walkspeed = hum.WalkSpeed
		if walkspeed < 20 and hum.MoveDirection.Magnitude > 0 then
			if lastState ~= "walk" then 
				walk:Play() 
			end
		elseif walkspeed > 20 then
			if lastState ~= "run" then 
				run:Play() 
			end
			lastState = "run"
		end
	else
		if lastState ~= "" then
			walk:Stop()
			run:Stop()
			lastState = ""
		end 
	end
	wait(.1)
end


1 Like