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.
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()
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
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