After respawning, the equip and idle animation for the tool doesn't play

Hello Developers!

As the title says, whenever I respawn with the tool, it’s equip and idle animation seems to not play for whatever reason.

Notes

  • The script is a local script.
  • If your wondering why the “OnUse” event is going to be called within the local script is because I want the item to go on cooldown after being used.
local ChickenLeg = script.Parent
--//Anims
local EquipAnim = ChickenLeg:WaitForChild("Equip")
local IdleAnim = ChickenLeg:WaitForChild("Idle")
--//Char
local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
--//Functionality
local OnUseEvent = ChickenLeg:WaitForChild("OnUseEvent")
local OnUse = false

ChickenLeg.Equipped:Connect(function()
	local EquipAnimTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(EquipAnim)
	EquipAnimTrack:Play()
	task.wait(0.22)
	
	local IdleAnimTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(IdleAnim)
	IdleAnimTrack:Play()
	ChickenLeg.Unequipped:Connect(function()
		IdleAnimTrack:Stop()
	end)
end)

ChickenLeg.Activated:Connect(function()
	if not OnUse then
		OnUse = true
		
		local StarterGUI = game:GetService("StarterGui")
		StarterGUI:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
		
		OnUseEvent:FireServer()
	end
end)

OnUseEvent.OnClientEvent:Connect(function()
	local StarterGUI = game:GetService("StarterGui")
	StarterGUI:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
	
	task.wait(7)
	OnUse = false
end)
1 Like

Since the character gets replaced after respawn you need to refer to the new character and humanoid:

Plr.CharacterAdded:Connect(function(newchar) -- runs every time the player respawns
Char = newchar -- replace with new character
Humanoid = newchar:WaitForChild("Humanoid") -- replace with new humanoid
end) 

don’t do player.CharacterAdded:Wait() . Place it above the player.Character. So it waits before continuing the script and you will always have the character. If it doesn’t work. Add prints and check where it doesn’t work.

Due to race conditions, the script may run after the player’s character spawned, which causes the script to yield until the player dies and respawns.
Along with that player.CharacterAdded:Wait() also returns the character when it gets fired