LoadAnimation not working?

For some reason when I click this button it won’t play the animation and yes the animation priory is set to to Core.

local id = script.Parent.Name
local anim = Instance.new("Animation")
anim.Parent = script.Parent
anim.AnimationId = "rbxassetid://"..id
local track = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(anim)
local db = false

script.Parent.MouseButton1Click:Connect(function()
	if db == false then
		db = true
		track:Play()
	else
		db = false
		track:Stop()
	end
end)

while true do
	wait()
	for i = 1, 840 do
		local irox = script.Parent.ImageRectOffset.X
		local iroy = script.Parent.ImageRectOffset.Y
		script.Parent.ImageRectOffset = Vector2.new(irox +0.5, iroy +0.5)
		wait()
	end
	wait()
	script.Parent.ImageRectOffset = Vector2.new(0, 0)
end

image

This is probably the cause of your issue. Try setting the priority to action instead.

From the developer hub article on the animation editor:

1 Like

As @realhigbead said, the cause is likely that you set the priority to core.

AnimationPriority

Core is the lowest priority, so any Roblox animations that are playing, or other animations you may be playing are likely taking a higher priority.

Edit: Added picture for anyone who doesn’t feel like following the link. :stuck_out_tongue_closed_eyes:

2 Likes

The LocalPlayer’s character may be nil at the time of indexing it.
You should define it as a variable which would :Wait() on CharacterAdded if it is nil:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local track = character:WaitForChild("Humanoid"):LoadAnimation(anim)

That is a good catch, but he did post the hierarchy and he did not mention any errors. The local script is within the StarterGUI. Anything placed in the StarterGUI isn’t going to replicate until the Character is spawned.


Taken from the Developer Hub API References page.

Edit: In the case of the Character dying, he may want to add a check that the character is not equal to nil before attempting to play the animation.

1 Like

Ah yeah, forgot about that.
It’s a good idea to make it a variable anyways for later use.

Infact, they can just :Connect on CharacterAdded and update the character and track variables.

player.CharacterAdded:Connect(function(char)
    character = char
    track = character:WaitForChild("Humanoid"):LoadAnimation(anim)
end)

The GUIs and consequently the scripts inside get deleted when the character respawns as this is all inside the StarterGUI as I stated before. That event will never trigger in this case.

There’s three mistakes worth fixing here.

  1. Use a different animation priority. All character animations run at the Core priority and the Jump animation runs at the Idle priority. Your animation could be getting overwritten by default animations.

  2. Set properties before you parent.

  3. You have one too many waits in your while loop. At the very least, if all of them are needed, try to avoid yielding at the beginning of an iteration.

Not always, if ResetOnSpawn is set false then the event will be needed.

1 Like

Then I don’t think it would be an issue because it would wait until the Character is spawned before running the script at all. I normally don’t have issues of the Player being nil.

Yeah that’s what I said too, the other guy posted that suggestion.