How can I play an animation I made in the official Roblox editor?

With the official animation plugin built into studio I made a simple pistol reload animation. It’ll work good enough for a TPS.


The problem is, I’m not sure how to load this animation into the code of the gun, and to give an idea of what I’m trying to do imagine the code like this:

(Reload Code)
–Animation script goes here
wait(reloadTime)

I was checking the Roblox tutorial list, and the animation section, but it really wasn’t helpful for what I was doing because it didn’t really explain much besides “copy and paste this code here”.

If somebody could help me understand how I can play an animation on the character that’s holding the tool, that would be a huge help and greatly appreciated.

local animation = Humanoid.Animator:LoadAnimation(put your path to the animation object here)

Now that your animation is loaded into the humanoid, you can pretty much play it anytime (as long as it’s a variable in your script)

animation:Play()

You can use the LoadAnimation function that’s provided by the Animator object inside the Humanoid which will return back an AnimationTrack Instance that you can use to play your animation

You’d need to get the Player’s Character however to do that, and depending on what type of script you have it’ll vary,

  • Server Script you’d need to get the Character by using Parents and such

  • Local Script you can just define the Player & Character by doing local Character = Player.Character or Player.CharacterAdded:Wait()

When you start to reload, that’s when you should play your animation right then, wait until the Animation is fully finished then resort back to using your fire script:

Since you’re using a KeyframeSequence I’m assuming though, you’d need to create an animation, then get the ID, then prepare to load it (Or look at the code samples for this: KeyframeSequence | Roblox Creator Documentation)

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Character:WaitForChild("Animator")

--Do the reload code
local Animation = Instance.new("Animation")
Animation.AnimationID = "rbxassetid://0000000"
Animation.Parent = Character

local PlayAnim = Animator:LoadAnimation(Animation)
PlayAnim:Play()
2 Likes

Ah ok. So if it was a normal script I’d just parent until I get the player that has the tool in their backpack?

In this case I have a local script so that saves some time.

Yeah, if it was server you’d do

Tool.Activated:Connect(function()
    local Character = Tool.Parent
end)

Where the Tool is the script’s Parent, and the Character is the Tool’s Parent (Or script > Tool > Character)

For a LocalScript, yeah you can easily define the player like this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

Which one is better? (less lag, speed, loads quicker, etc) Player.Character or Player.CharacterAdded:Wait()?

Player.Character would be faster, but there is the possibility that it may error due to the Character not fully loading in right, so we use our second conditional statement CharacterAdded:Wait() if the Character is not already in the game

there was some conflicting local variables so I changed them with an extra letter r for reload. It should work alright I hope?



image
I did something wrong… oof.

Do you know what it means to “index nil with waitforChild?”

That’s just a case-sensitive issue? It should be WaitForChild()

Also you removed the parenthesis when you showed that image:
image

yeah i fixed the paranthesis
I should probably read up on WaitForChild tutorials or something, because I’m pretty clueless

You’re fine :sweat_smile:

There are a couple of API References that do give you a basic/general idea on what each thing does though

Just make sure to double-check your spelling errors, those can happen frequently

That’s really weird


it still doesn’t like that

U m

It should look something like this, you didn’t specify a specific parameter you want to find:

local rPlayer= game.Players.LocalPlayer
local Character = rPlayer.Character or rPlayer.CharacterAdded:Wait()
local rHumanoid = Character:WaitForChild("Humanoid")
local Animator = Character:WaitForChild("Animator")

local Animation = script.Parent.ReloadAnimation

image
I swear sometimes studio doesn’t like me

You aren’t using the rPlayer.CharacterAdded:Wait() event, which is why it keeps resulting in that error

You can’t heavily rely on just 1 value to define your variable, sometimes you might need an alternate if it doesn’t work

That seemed to work.
But now I got an error around the PlayAnim:play() part

Jeez im a handful…


Threw all the variables at the bottom of the script because it seems to interrupt the code at the top.

Okay what object is your Animation? A KeyframeSequence? An AnimationTrack? You might to create an Animation Object instead to implement it that way

(2) Pistol reload - Roblox

/\ link to animation I made

image
image


would that be a keyframe editor?