How would I rewrite this animation script?

Any efficient way to rewrite this code? Don’t want to rely on a script like this.

local Player = game.Players.LocalPlayer
task.wait(2)
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))
1 Like

Try this

local Player = game.Players.LocalPlayer
repeat task.wait() until Player.Character
local Character = Player.Character
local Humanoid = Character:WaitForChild(“Humanoid”)

local AnimHandler = Instance.new(“Animation”, script)
AnimHandler.AnimationId = “rbxassetid://AnimID”

local Anim = Humanoid.Animator:LoadAnimation(AnimHandler)

2 Likes

Well the main issue is that Humanoid:LoadAnimation is now humanoid. Animator:LoadAnimation.

Apart from that, those are the steps. You can load animations before you need them, which is how I get around all the waiting, but you will need an efficient way to manage the tracks.

This works, thank you, just had to modify a few variables

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

You didn’t need the wait(2) in the original script “CharacterAdded:Wait()” yields the current thread until the event is fired before continuing execution of the script.