Animation not playing

Hello! I was trying to test out an animation when I got this error:

Workspace.WolfieGamerYT.ProximityPrompt.Script:4: attempt to index nil with 'Character'

Here is my code:

local Players = game:GetService("Players")

local Player = Players:FindFirstChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Instance.new("Animation")
Animation.AnimationId = "https://web.roblox.com/library/14639655080/Punch"

local AnimationTrack = Humanoid:LoadAnimation(Animation)

script.Parent.Triggered:Connect(function()
	script.Parent:WaitForChild("Sound"):Play()
	Animation:Play()
end)

If you think you know went when wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

3 Likes

trying to get a specific player in a server script via player name isnt a good approach, since the script runs before the player object gets added, and sometimes the player isnt in the server, but you said you are testing.
For your testing, Use WaitForChild() instead.

By the way, you should make the animationtrack declared in the function.
like:

local Players = game:GetService("Players")

local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Instance.new("Animation")
Animation.AnimationId = "https://web.roblox.com/library/14639655080/Punch"



script.Parent.Triggered:Connect(function()
    local AnimationTrack = Humanoid:LoadAnimation(Animation)
	script.Parent:WaitForChild("Sound"):Play()
	Animation:Play()
end)
2 Likes

I’m not getting the error now, but the animation doesn’t play.

1 Like

I just realized, You are trying to play the animation object, you have to play the animation track.
Instead of “Animation:Play()” Do “AnimationTrack:Play()”

1 Like

try this

local Players = game:GetService("Players")

local Player = Players:FindFirstChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

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

local AnimationTrack = Humanoid:LoadAnimation(Animation)

script.Parent.Triggered:Connect(function()
	script.Parent:WaitForChild("Sound"):Play()
	AnimationTrack:Play()
end)

That fixed it also, but now I’m getting an error I’ve never seen before:

 Invalid animation id '<error: id not found in first 128 char for http AssetId>': 

What does this mean?

1 Like

Hm, Im not sure, ive never seen that error before.
Make sure that you own the animation ID you are trying to play, or make sure that the ID is in fact an animation.

1 Like

Perhaps you included the entire link, try just the numbers. or rbxassetid://(numbers)

Edit: If you don’t own the animation, use this plugin (Animation Spoofer [V2 Update!] - Roblox

2 Likes

Oh thats right, I wasnt looking at the animation ID itself, maybe that will work then?

I believe this will be the full script

local Players = game:GetService("Players")

local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

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



script.Parent.Triggered:Connect(function()
    local AnimationTrack = Humanoid:LoadAnimation(Animation)
	script.Parent:WaitForChild("Sound"):Play()
	Animation:Play()
end)
1 Like

I do own the animation, so that’s not the problem.

But now I get THIS error:

Workspace.WolfieGamerYT.ProximityPrompt.Script:5: attempt to index nil with 'WaitForChild'

I think it goes to this line:

local Humanoid = Character:WaitForChild("Humanoid")
1 Like

I see, Maybe the character hasnt been added, so try this:

local Players = game:GetService("Players")

local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

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



script.Parent.Triggered:Connect(function()
    local AnimationTrack = Humanoid:LoadAnimation(Animation)
	script.Parent:WaitForChild("Sound"):Play()
	Animation:Play()
end)

maybe replacing the FindFirstChild with Waitforchild will fix it

local Players = game:GetService("Players")

local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

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

local AnimationTrack = Humanoid:LoadAnimation(Animation)

script.Parent.Triggered:Connect(function()
	script.Parent:WaitForChild("Sound"):Play()
	AnimationTrack:Play()
end)
1 Like

He is already using WaitForChild to wait for the humanoid.

That seemed to fix it! Thank you guys so much for your help!

1 Like

Of course! Good luck with your game!

2 Likes

i didnt mean the humanoid but he’ve already fixed it

I just have one question: I also want a GUI label to appear whenever the prompt is triggered. Would that be better to do locally or should I just try it in a regular script?

1 Like

You could do the entire thing locally, since animations are replicated to the server.
But if it has to be server-sided, Then you can place the GUI in the Player’s “PlayerGui”

I tried doing it locally and server-sided, and you were right: locally is much better. Thank you and have a great day!

1 Like

No problem! And you too! and yes, Locally is always best when doing GUI-related stuff, allows you to do much more.

2 Likes