Although this code does technically work, I find myself wanting a better way to do this. Are there better ways to go about having custom animations when a tool is equipped, and if so what are they? (I appreciate all help whether it’s related to scripting or not. I have very little experience in the way of animating.)
Currently I use the following script in the tool to do it:
-- Variables --
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local animate = plr.Character.Animate
local animations = tool.Animations
-- Functions --
-- Change to new animations --
tool.Equipped:Connect(function()
animate.idle.Animation1.AnimationId = animations.Idle.AnimationId
animate.idle.Animation2.AnimationId = animations.Idle.AnimationId
animate.walk.WalkAnim.AnimationId = animations.Walk.AnimationId
end)
-- Revert to default animations --
tool.Unequipped:Connect(function()
animate.idle.Animation1.AnimationId = "rbxassetid://180435571"
animate.idle.Animation2.AnimationId = "rbxassetid://180435792"
animate.walk.WalkAnim.AnimationId = "rbxassetid://180426354"
end)
I find that there are several issues with this though, such as delayed animation switching times, it possibly not even being on the server right (probably because this is in a local script, so I can try solving this myself- I haven’t tried yet tbh.), and it would become a hassle to update and add new animations any time I did.
I want the script to also work with other tools as well and all I’d have to do is update the animation IDs according to the tool.
Here is a video of the issues I’m talking about:
1 Like
dont change the animationid u should start playing a new animation track with those new animations when the tool gets unequipped/stop when it gets unequipped (assuming ur using defualt roblox animations for everything else) changing the animationid is probably a bad idea
Make your current animation have a higher priority than core, and add an “animation” instance to somewhere where the player can access it, Create a variable etc. Then, when the player equips the tool just play the animation and stop playing it when the player unequips
I know that’s a thing and was how I originally intended to do that but wouldn’t that only work for the idle animation? How could I script it so it’d play the walking animation when their walking or the jumping animation when they’re jumping?
Using the animate script is not how you should do it. You must make the animations in the tool and MAKE SURE to load the animations and play them depending on the humanoid’s state, OTHERWISE IT WON’T WORK!!!
After a lot of fiddling around with stuff I procured this script:
-- Variables --
-- Variables --
local Humanoid = game:GetService("Players").LocalPlayer.Character.Humanoid
local animator = Humanoid.Animator
local tool = script.Parent
local animations = tool.Animations
local connections = {}
-- Tracks --
local tracks = {
idle = animator:LoadAnimation(animations.Idle),
walk = animator:LoadAnimation(animations.Walk),
run = animator:LoadAnimation(animations.Run),
jump = animator:LoadAnimation(animations.Jump)
}
-- Functions --
local function playAnimation(animation, fadeTime)
for name, track in tracks do
if name == animation then
track:Play(fadeTime)
else
track:Stop(fadeTime)
end
end
end
local function onEquipped()
-- Play animations depending on state --
local stateConnection = Humanoid.StateChanged:Connect(function(state)
if Humanoid:GetState() == Enum.HumanoidStateType.Jumping then
playAnimation("jump", 0.2)
end
end)
table.insert(connections, stateConnection)
-- Play idle/walk animations --
local runningConnection = Humanoid.Running:Connect(function(speed)
if speed > 9 and speed < 28 then
playAnimation("walk", 0.2)
elseif speed > 28 then
playAnimation("run", 0.2)
else
playAnimation("idle", 0.2)
end
end)
table.insert(connections, runningConnection)
Humanoid:ChangeState(Enum.HumanoidStateType.Landed)
end
local function onUnequipped()
-- Disconnect all connections and reset table --
for _,connection in connections do
connection:Disconnect()
end
connections = {}
playAnimation(nil, 0.1)
end
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
It works significantly better than my previous method. I still think it could be better though, so what do y’all think?
Edit: Updated script just for better readability. I also noticed a side-effect/bug that is noticeable - when it’s switching between animations the player’s arms snap around (see attached video). Supposedly this issue is fixed by using the fadeTime in the :Play() and :Stop() functions but I already use that, and it still does it.