I have a script, everything works, but whenever the player dies the animation just stops playing.
–The Script
game.Players.PlayerAdded:Connect(function (plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid
if hum then
local animator = hum:FindFirstChild("Animator")
local animTrack = animator:LoadAnimation(game.ReplicatedStorage.Animations.Running)
local animTrack2 = animator:LoadAnimation(game.ReplicatedStorage.Animations.WeaponRun)
local C = coroutine.create(function ()
while task.wait(0.1) do
print(hum.WalkSpeed)
if hum then
if hum.WalkSpeed > 16 and not hum.Sit and not char:FindFirstChild("Tool") then
if animTrack.IsPlaying == false then
animTrack:Play()
animTrack:AdjustSpeed(2)
end
elseif hum.WalkSpeed > 16 and not hum.Sit and char:FindFirstChild("Tool") then
if not animTrack2.IsPlaying then
animTrack2:Play()
animTrack2:AdjustSpeed(2)
end
else
animTrack:Stop()
animTrack2:Stop()
end
end
end
end)
coroutine.resume(C)
end
end)
Like what pink sheep said, the original humanoid the player joined the game with is destroyed and replaced with a new one when the player dies, so something like this might work better instead;
game.Players.PlayerAdded:Connect(function (plr)
plr.CharacterAdded:Connect(function(char)
local hum = char.Humanoid
if hum then
local animator = hum:FindFirstChild("Animator")
local animTrack = animator:LoadAnimation(game.ReplicatedStorage.Animations.Running)
local animTrack2 = animator:LoadAnimation(game.ReplicatedStorage.Animations.WeaponRun)
local C = coroutine.create(function ()
while task.wait(0.1) do
print(hum.WalkSpeed)
if hum then
if hum.WalkSpeed > 16 and not hum.Sit and not char:FindFirstChild("Tool") then
if animTrack.IsPlaying == false then
animTrack:Play()
animTrack:AdjustSpeed(2)
end
elseif hum.WalkSpeed > 16 and not hum.Sit and char:FindFirstChild("Tool") then
if not animTrack2.IsPlaying then
animTrack2:Play()
animTrack2:AdjustSpeed(2)
end
else
animTrack:Stop()
animTrack2:Stop()
end
end
end
end)
coroutine.resume(C)
end
end)
end)
The ‘CharacterAdded’ event fires whenever the player spawns or respawns, it fires as soon as the Player.Character value is no longer nil. You can get the current humanoid instance the player is using through this.