Animation wont play after reset

My Animations won’t load after resetting, now i’ve searched around the forum for a solution and found multiple topics about it, however the scripts in those topics are either very unsimiliar with mine or has a different error, I gave up looking for a similar topic since there isn’t many about this. here is my animation script

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local KeyHeld = false

if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://6449883368"
local AnimTrack = animator:LoadAnimation(Anim)
AnimTrack.Priority = Enum.AnimationPriority.Action

function onKeyPress(inputObject,gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.C and not gameProcessed then
		KeyHeld = true	
		while KeyHeld do	
			AnimTrack:Play()
			humanoid.WalkSpeed = 0
			wait()
		end
	end
end

function onKeyRelease(inputObject,gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.C then
		KeyHeld = false
		AnimTrack:Stop()
		humanoid.WalkSpeed = 16
		wait()
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputEnded:connect(onKeyRelease)

script is stored in the StarterPlayerScripts

StarterPlayerScripts wont readd the script when you reset, and the character variable stores the now deleted player character, you need to put a CharacterAdded event somewhere in your code to overwrite the current character so it contains the new character and edit other variables that use it, add this somewhere in that script

player.CharacterAdded:Connect(function(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
	animator = humanoid:WaitForChild("Animator")
	
	AnimTrack = animator:LoadAnimation(Anim)
	AnimTrack.Priority = Enum.AnimationPriority.Action
end)

So it updates the variables on respawn

2 Likes

Aight thanks for your answer, I guess ive been doing it wrong since i was using Players and not player and was only using it for character and humanoid.

1 Like

You’d have to also load in the aniamtino again since the loaded animation was only for the new deleted humanoid.

If you have anymore issues don’t be afraid to make another post!

2 Likes