Animation script not playing animations correctly

This script will only play the WalkAnim, and will not play anything else despite playinganim changing.

local animate = script.Parent.Humanoid:WaitForChild("Animator")
local animations = {}
local function loadanim(id,name)
	if name == nil then
		name = id
	end
	local animation = Instance.new("Animation")
	animation.AnimationId = id
	local anim1 = animate:LoadAnimation(animation)
	anim1.Name = name
	--table.insert(animations,anim1)
	return anim1
end
local anim1 = loadanim("rbxassetid://110780195870689","WalkAnim")
local anim2 = loadanim("rbxassetid://73914823009571","RunAnim")
local anim3 = loadanim("rbxassetid://121213532889412","IdleAnim")
anim1:Play()
local function stopallanimationsinlist(list)
	for i,v in pairs(list) do
		v:Stop()
	end
end
local function StopAllAnimations()
	for i,animation in pairs(animations) do
		animation:Stop()
	end
end
local currentlyplaying = anim1
game["Run Service"].Heartbeat:Connect(function()
	local playinganim = anim1
	local magnitude = script.Parent.HumanoidRootPart.AssemblyLinearVelocity.Magnitude
	print(magnitude)
	if magnitude > 20 then
		playinganim = anim2
	end
	if magnitude < 1 then
		playinganim = anim3
	end
	local allanimexceptplayingamin = animations
	print(currentlyplaying)
	table.remove(allanimexceptplayingamin,table.find(allanimexceptplayingamin,playinganim))
	if currentlyplaying ~= playinganim then
		currentlyplaying = playinganim
		StopAllAnimations()
		playinganim:Play()
		print(playinganim)
	end
	if currentlyplaying == anim1 then
		anim1:AdjustSpeed(script.Parent.HumanoidRootPart.AssemblyLinearVelocity.Magnitude/18)
	end
end)

It should change the animation to playinganim, but instead it just doesn’t do anything.

1 Like

I fixed it, turns out the problem is with me not uncommenting table.insert(animations,anim1), after changing that it works fine.

updated code:

local animate = script.Parent.Humanoid:WaitForChild("Animator")
local animations = {}
local function loadanim(id,name)
	if name == nil then
		name = id
	end
	local animation = Instance.new("Animation")
	animation.AnimationId = id
	local anim = animate:LoadAnimation(animation)
	anim.Name = name
	table.insert(animations,anim)
	return anim
end
local anim1 = loadanim("rbxassetid://110780195870689","WalkAnim")
local anim2 = loadanim("rbxassetid://73914823009571","RunAnim")
local anim3 = loadanim("rbxassetid://121213532889412","IdleAnim")
--anim1:Play()
local function stopallanimationsinlist(list)
	for i,v in pairs(list) do
		v:Stop()
	end
end
local function StopAllAnimations()
	for i,animation in pairs(animations) do
		animation:Stop()
	end
end
local playinganim = nil
local currentlyplaying = nil
game["Run Service"].Heartbeat:Connect(function()
	playinganim = anim1
	local magnitude = script.Parent.HumanoidRootPart.AssemblyLinearVelocity.Magnitude
	print(magnitude)
	if magnitude > 20 then
		playinganim = anim2
	end
	if magnitude < 1 then
		playinganim = anim3
	end
	if currentlyplaying ~= playinganim then
		currentlyplaying = playinganim
		StopAllAnimations()
		playinganim:Play()
		print(playinganim)
	end
	if currentlyplaying == anim1 then
		anim1:AdjustSpeed(magnitude/18)
	end
end)

TLDR: I’m an idiot

1 Like

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