Essentially, after respawning, the script attempts to access the old character, so it ends up not working on the new character. To counteract this, you’d want to re-make the variable, and you can do this by wrapping the function with the CharacterAdded function. With it, everytime the player respawns, we’ll access the new character instead of using an old variable.
I’ll add an example, but without altering your code entirely, rather just adding the function as an example.
So, this should work instead.
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
player.CharacterAdded:Connect(function(Char)
local char = player.Character or player.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local anim = h:LoadAnimation(script.Parent:WaitForChild("Animation"))
tool.Equipped:Connect(function()
anim:Play()
end)
tool.Unequipped:Connect(function()
anim:Stop()
end)
end)
Your post is fine, but you’ll want to use codeblocks in the future when posting snippets of your own code. To use these, wrap ``` between your code, with one at the top and the other at the bottom.
What Disrespectly posted is why you have to resetup the character each time it respawns
But on his code if the character is already in game the characteradded will not fire and will not setup the character with animation. (His script may work fine for the way you are loading it.) To fix this you would use a function to setup the character each time and call it from that function – since this is a local script in a tool equip is where you can setup the animation each time
here is the script that should load animation each time even after character respawn because it gets the character each time to make sure the animation track is setup
local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local anim -- this will hold the animation track once its equipped and setup
Tool.Equipped:Connect(function()
local Character = Player.Character or Player.CharacterAdded:Wait() -- get the character
local Humanoid = Character:FindFirstChild('Humanoid') -- get humanoid for load of animation track
anim = Humanoid:LoadAnimation(script.Parent:WaitForChild("Animation")) -- load the track and set to variable
anim:Play()
end)
Tool.Unequipped:Connect(function()
if not anim then return end -- if its not setup yet just return
anim:Stop()
end)