Why does my animation not play?

Hello! So I have a tool which opens a UI (singular button). When you click the button, the local script fires an event to a server script (located in ServerScriptService) which plays the animation provided. The animations loop in order.

The problem: My animations are not played. Nothing happens. No errors.

Local Script

local player = game.Players.LocalPlayer
local event = game:GetService("ReplicatedStorage"):WaitForChild("animSwitch")

-- Animations removed for DevForum

local animations = {
	[1] = "rbxassetid://[]",
	[2] = "rbxassetid://[]",
	[3] = "rbxassetid://[]",
	[4] = "rbxassetid://[]",
}

local currentAnim = nil

script.Parent.MouseButton1Click:Connect(function()
	if currentAnim == nil then
		event:FireServer(animations[1])
		currentAnim = 1
		print("Playing animation "..currentAnim)
	elseif currentAnim ~= nil then
		local nextAnim, value = next(animations, currentAnim)
		event:FireServer(value)
		currentAnim = nextAnim
		print("Playing animation "..currentAnim)
	end
end)

Server Script

game:GetService("ReplicatedStorage"):WaitForChild("animSwitch").OnServerEvent:Connect(function(player, animationId)	
	local character = player.Character
	local humanoid = character.Humanoid

	if not humanoid then warn("no humanoid") end
	
    local batonAnimation = Instance.new("Animation")
	batonAnimation.AnimationId = animationId

	local batonTrack = humanoid:LoadAnimation(batonAnimation)
	batonTrack:Play()
end)

The code prints stuff into the output, but nothing happens. Does anyone know why?

1 Like

You are able to play animations from the client, and have it replicated to the server. I would recommend putting the animation “handlers” (loading the animation & playing it) from the server script into the client-sided script.

1 Like

Does the animation replicate directly to the server if I play it from the client, or is there something I need to do in order to replicate the animation to the server?

Also, in terms of handling animations, you mean loading them into an animation instance within the client-sided script rather than the server script?

It does replicate automatically, yes.

By “handling animations”, I mean putting this part

	local character = player.Character
	local humanoid = character.Humanoid

	if not humanoid then warn("no humanoid") end
	
    local batonAnimation = Instance.new("Animation")
	batonAnimation.AnimationId = animationId

	local batonTrack = humanoid:LoadAnimation(batonAnimation)
	batonTrack:Play()

from the server script into the local script.
Obviously, please remember to change and set variables where necessary.

Right, so I’ve edited my local script so it contains the following:

local player = game.Players.LocalPlayer
-- local event = game:GetService("ReplicatedStorage"):WaitForChild("animSwitch")

local animations = {
	[1] = "rbxassetid://[]",
	[2] = "rbxassetid://[]",
	[3] = "rbxassetid://[]",
	[4] = "rbxassetid://[]",
}

local currentAnim = nil

local function playAnimation(animationId)
	local character = player.Character
	local humanoid = character.Humanoid

	if not humanoid then warn("no humanoid") end

	local batonAnimation = Instance.new("Animation")
	batonAnimation.AnimationId = animationId

	local batonTrack = humanoid:LoadAnimation(batonAnimation)
	batonTrack:Play()
end


script.Parent.MouseButton1Click:Connect(function()
	if currentAnim == nil then
		playAnimation(animations[1])
		currentAnim = 1
		print("Playing animation "..currentAnim)
	elseif currentAnim ~= nil then
		local nextAnim, value = next(animations, currentAnim)
		playAnimation(value)
		currentAnim = nextAnim
		print("Playing animation "..currentAnim)
	end
end)

When running the game and clicking the UI, nothing happens, but the output says it’s playing an animation.

https://streamable.com/a1oz51

Try setting the parent of batonAnimation to script

I set batonAnimation’s parent to script (meaning now its located within StarterGui in the script…) and nothing happened.

Screenshot 2023-11-10 at 7.18.01 PM

I meant to do

batonAnimation.Parent = script

If you’ve done this, try setting the parent to the humanoid instead.

Yep, did both of those and I’m still not getting any results… :((

local function playAnimation(animationId)
	local character = player.Character
	local humanoid = character.Humanoid

	if not humanoid then warn("no humanoid") end

	local batonAnimation = Instance.new("Animation")
	batonAnimation.Parent = humanoid
	batonAnimation.AnimationId = animationId

	local batonTrack = humanoid:LoadAnimation(batonAnimation)
	batonTrack:Play()
end

I’ve done a little research, and have come to the conclusion that Humanoid:LoadAnimation() is deprecated and should no longer be used.

You should read the following articles to familiarise yourself with the Animator class:

Here is a code snippet from the Animator documentation which may prove helpful:

local function playAnimationFromServer(character, animation)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		-- need to use animation object for server access
		local animator = humanoid:FindFirstChildOfClass("Animator")
		if animator then
			local animationTrack = animator:LoadAnimation(animation)
			animationTrack:Play()
			return animationTrack
		end
	end
end

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"

local character = script.Parent

playAnimationFromServer(character, animation)
local function playAnimation(animationId)
	local character = player.Character
	local humanoid = character.Humanoid

	if not humanoid then warn("no humanoid") end

	local animator = humanoid:WaitForChild("Animator")

	if animator then
		local batonAnimation = Instance.new("Animation")
		batonAnimation.Parent = humanoid
		batonAnimation.AnimationId = animationId

		local batonTrack = animator:LoadAnimation(batonAnimation)
		batonTrack:Play()
		print("Playing animation "..currentAnim)
	end
end

I’ve adjusted my code to the following, and surprisingly the code prints the string at the end, meaning the animator exists, yet the actual animation still doesn’t play… I’m confused lol.

Fixed! My animation was set for R15 but my avatar was R6, thus the animations would not play.

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