Tool animation problem

so i have

local Tool = script.Parent
local AnimationFolder = ReplicatedStorage:WaitForChild("AnimationFolder")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()
while Character.Parent == nil do
	Character.AncestryChanged:wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
local Stab = Humanoid:WaitForChild("Animator"):LoadAnimation(AnimationFolder.Stab)

i use

while Character.Parent == nil do
	Character.AncestryChanged:wait()
end

to try to get around the issue where you cant load animation if your character is not in the workspace but it seam to just wait for ever I try other methods and print the character parent it will tell me its nil even do my character is in the workspace

2 Likes

Add a break after the while loop.

if i do then im back to the error were it tells me that

“LoadAnimation requires the Humanoid object to be a descendant of the game object”

So the humanoid is nil??? This text will be blurred

no it mean that the player character is not parented to workspace
that why i made a loop to wait till character parent is not nil

That makes no sense. Player objects are in the Player part of the game, not the workspace?

the isue is that to use LoadAnimation the humanoid need to be descendent of workspace but I put the tool in the backpack and ig the script run before the game load my character in so i made a loop to wait for character but all the loop i try make always tell me the character parent is nil even do I’m in the game

Okay, I’m making a redo of this script. In the while loop, we’re checking if the players character is nil? (Nothing.) That doesn’t make sense.

my loop wait for the character parent not to be nil witch will mean it will be parented to workspace

Something can have a parent yet still not be a descendant of workspace. Try this:

while not Character:IsDescendantOf(workspace) do
    Character.AncestryChanged:Wait()
end

Debugging tip: For the sake of debugging, you could print GetFullName on the character before each wait() to inspect what it might be inside.

while not Character:IsDescendantOf(workspace) do
    print("Not yet in workspace: ", Character:GetFullName(), Character.Parent)
    Character.AncestryChanged:Wait()
end
local Tool = script.Parent
local AnimationFolder = game.ReplicatedStorage:WaitForChild("AnimationFolder")
local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

while Character.Parent == nil do
	Character.AncestryChanged:Wait()
end

local Stab = Humanoid:WaitForChild("Animator"):LoadAnimation(AnimationFolder.Stab)

Some capitalization, and other things were incorrect. Try this, it might work.

I try something like this and it just loops forever if I print

Character:IsDescendantOf(workspace)

it will say false even do I’m loaded in

this give me same result it wait for ever

So lets go back to the error.

This means that the Humanoid of the character needs to be a descendant of the game object before LoadAnimation can be called.

try to put your tool inside playerCharacterScripts

yes that the reason i wait for the character to be loaded in so that i can load the animation

The tool code only grabs a reference to the player’s character once, so if the character changes (ie. the player respawns, maybe even immediately) your tool will still be considering the old character. Since this is a tool, you should load the AnimationTrack when the player equips the tool, as you can just reference the character using Tool.Parent.

local Tool = script.Parent
local AnimationFolder = game.ReplicatedStorage:WaitForChild("AnimationFolder")

local stabAnimTrack = nil
local function onEquipped()
    stabAnimTrack = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Stab)
end

local function onUnequipped()
    stabAnimTrack = nil
end

Tool.Equipped:Connect(onEquipped)
Tool.Unequipped:Connect(onUnequipped)

that work but that also mean they will start holding the tool… i have multiple start up tools they cant start whit holding 7 tools

So we could use repeat() task.wait(0.01) until Character.Humanoid || This would wait until the Characters humanoid exists.

well i can use this for now i wanted to not have to load animation each time the player equip the tool