When My Character dies LoadAnimation gives Error and My Tool Doesn't Work

Hello when my character dies loadanimation gives error and my tool doesn’t work. The tool is in StarterPack so i don’t really know why it happens.
the error: LoadAnimation requires the Humanoid object (Flacion.Humanoid) to be a descendant of the game object - Client - CombatLocalScript:14
Stack Begin - Studio
Script ‘Players.Flacion.Backpack.Combat.CombatLocalScript’, Line 14

the script:

--RemoteEvent --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.CombatSystem:WaitForChild("Combat")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunningRemoteEvent = ReplicatedStorage:WaitForChild("Running")
-- Character and Humanoid --
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

--StanceAnimation--
local tool = script.Parent
local StanceAnimation = tool:WaitForChild("CombatStance")
local LoadedStanceAnimation = Humanoid:LoadAnimation(StanceAnimation)

--Stance--
tool.Equipped:Connect(function()
	LoadedStanceAnimation:Play()
end)
tool.Unequipped:Connect(function()
	LoadedStanceAnimation:Stop()
end)


try to update it each time it’s eqquipped

--RemoteEvent --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.CombatSystem:WaitForChild("Combat")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunningRemoteEvent = ReplicatedStorage:WaitForChild("Running")
-- Character and Humanoid --
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

--StanceAnimation--
local tool = script.Parent
local StanceAnimation = tool:WaitForChild("CombatStance")
local LoadedStanceAnimation

--Stance--
tool.Equipped:Connect(function()
	Humanoid = script.Parent.Parent.Humanoid
	LoadedStanceAnimation = Humanoid:LoadAnimation(StanceAnimation) --update the variable
	LoadedStanceAnimation:Play()
end)
tool.Unequipped:Connect(function()
	LoadedStanceAnimation:Stop()
end)

--Gerekliler

local debounce = false  

tool.Activated:Connect(function()
	if debounce == false and Character:FindFirstChild("Stun") == nil and Character:FindFirstChild("Dashing") == nil  then
		debounce = true
		RemoteEvent:FireServer()
		RunningRemoteEvent:FireServer(false)

	end

end)

RemoteEvent.OnClientEvent:Connect(function()
	debounce = false
end)
1 Like

Two things:

1- Don’t load the animation through the humanoid, use an animator instead as the humanoid method is deprecated and could stop working, it won’t be maintained.

2- You have to get the humanoid every time the tool is equipped

tool.Equipped:Connect(function()
    local humanoid = tool.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
        local animator = humanoid:FindFirstChild('Animator') or Instance.new('Animator')
        animator.Parent = humanoid
        loadedStanceAnimation = loadedStanceAnimation or animator:LoadAnimation(stanceAnimation)
        loadedStanceAnimation:Play()
    end
end)
3 Likes

This is because the character has died and the character instance is removed and thus, the humanoid is no longer an object of the character.
Maybe do:

humanoid.Died:Connect(function()
loadstance:Destroy()
end)

Since the character instance isn’t removed instantly.

1 Like

I should do the same when animation plays when i activate the tool too right?

You should add animation in a loop so it will update per second and it have to check humanoid not equal to nil for no error.

Like:
while wait(1) do
if Humanoid~= nil then
local LoadedStanceAnimation = Humanoid:LoadAnimation(StanceAnimation)
end
end

—your code

1 Like