Help with playing idle animation

I have created an idle animation for whenever a sword is equipped but I don’t know how to play it without using ‘LoadAnimation()’.
I would really appreciate nay help regarding playing these animations using the animator.

Why do you not want to use LoadAnimation???

Because ‘LoadAnimation()’ is deprecated ( Humanoid | Roblox Creator Documentation ).
You cannot use it now.

You still can use it, however the new method they introduced can be found here. It has an example of how to use it at the bottom of the page.

I tried this but the animation is still not working :frowning_face: I must have written something wrong in the code but we can play animations using the animator also right ? I also need this to be an idle animation so this needs to be looped.

At times like these, showing your code and any error messages in output would be a good idea.

oh yeah sorry.

local player = game:GetService(“Players”).LocalPlayer
local character = player.character
local humanoid = character:FindFirstChildOfClass(“Humanoid”)
if humanoid then
local animator = humanoid:FindFirstChildOfClass(“Animator”)
if animator then
local animationTrack = animator:LoadAnimation(7093739542)
animationTrack:Play()
return animationTrack
end
end

I made this as a local script inside the sword tool.

1 Like

Try this.

local player = game.Players.LocalPlayer
local character = player.character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")

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

if humanoid then
	local animator = humanoid:FindFirstChildOfClass("Animator")
	if animator then
		local animationTrack = animator:LoadAnimation(animation)
		animationTrack:Play()
		return animationTrack
	end
end

Fixed it, it should work now

1 Like

I’m sorry but could you put your code inside of a code block using ```

Also, taking a look at the code itself, it doesn’t look like you’re actually checking when the player equips the tool?
I was just pointing out that there was code showing you how the new animator class works, it doesn’t actually show you how to play an animation when the player equips a tool, don’t copy it.

Here’s an example I made, don’t know if it properly works.
(Put it inside of the tool)

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character

local tool = script.Parent

-- animation stuff
local animation = Instance.new("Animation")
animation.AnimationId = "Your animation ID here"

local animator = character.humanoid:FindFirstChildOfClass("Animator")  -- The animator inside of character humanoid
local IdleAnimation = animator:LoadAnimation(animation)  -- load the animation onto the animator

tool.Equipped:Connect(function()  -- when tool is equipped
    IdleAnimation:Play()  -- play idle animation
end)
2 Likes

I am really sorry , I am a 3D modeller and I just wanted to make a game randomly so I am just learning LUA right now. Thank you very much for the help.

1 Like

There’s no shame in asking for help, everybody starts somewhere.

1 Like

I am sorry I am disturbing you again but this is happening.

My mistake I had some typos in my original code that I’ll fix.

  1. Capitalize “animation” in Instance.new("animation")

  2. Change the line tool.equipped to tool.Equipped:Connect(function()

1 Like

It is coming that 'local player is not valid member of Players “Players” ’

Oh right, it should be LocalPlayer with the l and p capitalized.

Is this even a right animation that I have made for idle because as soon as you tell me to fix something another issue happens.

What the issue? If it’s that the animation isn’t playing, be sure to change its animation priority to “action”

Your issue has nothing to do with this. When giving an animation a Id you need to add “rbxassetid://” to make sure Roblox recognizes the id from your inventory.

e.g

animation.AnimationId = "rbxassetid://7093739542"

Here’s an example of a working idle script I make these all the time, so this should work.

local tool = script.Parent -- inside of the tool
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://IDHERE"
local idleTrack = animator:LoadAnimation(animation)

tool.Equipped:Connect(function()
	if not idleTrack.IsPlaying then
		idleTrack:Play()
	end
end)
tool.Unequipped:Connect(function()
	if idleTrack.IsPlaying then
		idleTrack:Stop()
	end
end)
1 Like

This is a local script right inside the tool ?

Yessir, that’s where it should go. Make sure to add the ID as well. I hope you remember this, so you don’t run into the same problem.

1 Like