Animation system working when player joins game but when they reset it breaks

I’ve made an animation script that would play an animation when a player says a phrase but if they reset the script no longer works. I’m not sure how to make it work when the player does reset so that’s what I need help with.

game:GetService("Players").PlayerAdded:Connect(function(Player)	
	local character = Player.Character
		repeat wait()
			character = Player.Character
		until character
	local Humanoid = character:WaitForChild("Humanoid")
	local HRP = character:FindFirstChild("HumanoidRootPart")
	local LevitateAnimation = Humanoid:LoadAnimation(LevitateSit)
	local LevClone = LevitateSound:Clone()
		LevClone.Parent = HRP
		LevClone.Looped = true
		
	Player.Chatted:Connect(function(Message)
				
		if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
			if Message == "/e LevitateSit" then
				LevClone:Play()
				LevitateAnimation:Play()			
			end
			Humanoid.Jumping:Connect(function()				
				LevClone:Pause()
				LevitateAnimation:Stop()
			end)
			Humanoid.Running:Connect(function()
				LevClone:Pause()
				LevitateAnimation:Stop()
			end)
		end
	end)
end)

You need to make sure that the animation is initiated everytime the player’s character is loaded (even after dying). You can do that with the .CharacterAdded function.

game:GetService("Players").PlayerAdded:Connect(function(Player)	
	Player.CharacterAdded:Connect(function(character) -- Character added, load animation
		local Humanoid = character:WaitForChild("Humanoid")
		local HRP = character:FindFirstChild("HumanoidRootPart")
		local LevitateAnimation = Humanoid:LoadAnimation(LevitateSit)
		local LevClone = LevitateSound:Clone()
		LevClone.Parent = HRP
		LevClone.Looped = true
	end)

	Player.Chatted:Connect(function(Message)
		if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
			if Message == "/e LevitateSit" then
				LevClone:Play()
				LevitateAnimation:Play()			
			end
			Humanoid.Jumping:Connect(function()				
				LevClone:Pause()
				LevitateAnimation:Stop()
			end)
			Humanoid.Running:Connect(function()
				LevClone:Pause()
				LevitateAnimation:Stop()
			end)
		end
	end)
end)
1 Like

Ah ok I see now. Thank you very much for the help!