Why Wont My Animation Play?

Hello devs! I am making a game that requires you to hit things with a baseball bat. I am currently making the tool with an animation that plays on activation/click. This is a very simple script and should be working.

local tool = script.Parent
local animation = tool:WaitForChild("Animation")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")


local animationTrack = humanoid:LoadAnimation(animation)

tool.Activated:Connect(function()
	if not animationTrack.IsPlaying then
		animationTrack:Play()
	end
end)

image

Hello :waving_hand:
Did you try using the animation controller or player thing in the humanoid? (Sorry its 3 am I’m tired)

hah no your fine! sorry to ask this but how exactly would i go about that? (im bad at aniamtions)

I think it would go like ```local tool = script.Parent
local animation = tool:WaitForChild(“Animation”)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild(“Humanoid”)

local animationController = humanoid:FindFirstChild(“AnimationController”) – it might not be named this but just playtest and look in your humanoid and you should find it

local animationTrack animationController:LoadAnimation(animation)-- again might not be called this funtion

tool.Activated:Connect(function()
if not animationTrack.IsPlaying then
animationTrack:Play()
end
end)```
Sorry if this is confusing i am typing this on my phone

ahh its fine its late here too. if its 3 you should prob go to sleep LOL! ima head to bed soon too

Np bro!

30charlimittttttttttttt

Here is a few steps I take when playing animations on the client with tools

local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

AnimationList = {
	Idle = {Animation = Tool:WaitForChild("Idle")},
	Lunge = {Animation = Tool:WaitForChild("Lunge")},
}

function PlayAnimation(animationData)
	local entry = AnimationList[animationData.Name]
	if not entry then return end
	
	if ToolEquipped and Humanoid.Health > 0 then
		if entry.AnimationTrack then
			print("Stopping")
			while entry.AnimationTrack.IsPlaying do
				entry.AnimationTrack:Stop()
				task.wait()
			end
			print("  stopped")
		else
			entry.AnimationTrack = Animator:LoadAnimation(entry.Animation)
			print("call loading")
			while entry.AnimationTrack.Length == 0 do 
				task.wait() 
			end
			print("loaded, ",entry.AnimationTrack.Length)
		end
		if animationData.Priority then
			entry.AnimationTrack.Priority = animationData.Priority
		end
		while not entry.AnimationTrack.IsPlaying do
			print(".. check play")
			entry.AnimationTrack:Play(animationData.FadeTime, animationData.Weight, animationData.Speed)
			task.wait(.3)
		end
		if animationData.StartTime then
			entry.AnimationTrack.TimePosition = animationData.StartTime
		end
		print(" playing!")
	end
end

function StopAnimation(animationData)
	local entry = AnimationList[animationData.Name]
	if not entry then return end

	if entry.AnimationTrack then
		print("Stopping")
		while entry.AnimationTrack.IsPlaying do
			entry.AnimationTrack:Stop(animationData.FadeTime)
			task.wait()
		end
		print("  stopped")
	end
	
end


function Equipped(Mouse)
	ToolEquipped = true
	PlayAnimation({Name = "Idle"})
end

function Unequipped()
	ToolEquipped = false
	for k,v in pairs(AnimationList) do
		StopAnimation({Name=k})
	end
end

function Activated()
	if not Tool.Enabled or not ToolEquipped or Humanoid.Health <= 0 then
		return
	end
	Tool.Enabled = false
	SoundList.Lunge:Play()
	PlayAnimation({Name = "Lunge"})
	wait(1)
	Tool.Enabled = true
end

1 Like

Instead of using the Humanoid for loading AnimationTracks, use the Animator for loading AnimationTracks.

If you want all players to see the animation, you should rely on the server instead.

1 Like

Any animation played on a client on an animator under the humanoid will replicate to all clients.

So if its an NPC, then yes, play on the server.
However if its a tool, or something playing on your character, play on the client.

1 Like

Really? I always thought animations will replicate to all clients if you ran it on the server.
Thanks, I didn’t know that!

You are correct, animations on the Server, WILL replicate to all clients, thats why you use them for NPC animtions.

However, for character animations on the Client, they too WILL replicate to all clients, however, it plays on the characters machine first, which makes the animation smooth due to it not having to come from the the server first.

So, if you press the R key, and your character does a gun reloading animation.
If you do it on the server, then…

  1. client detects keypress, and sends message to the server to play animation
  2. server plays animation
  3. animation is replicated back to client. and to all other clients

However, if you play the animation on the client it would be…

  1. client detects keypress and plays animation
  2. animation is replicated to the server
  3. server sends animation to all other clients

The second way you get to see the animation instantly when you press the key.