Animation GUI breaks after player reset

I have a dance GUI that breaks after a player reset. There are no errors, I’ve tried multiple other solutions, but none of worked. Unfortunately, I’m trying to figure this out in the middle of the night and I’m failing with every turn. Any help is greatly appreciated, thanks!

The script:

wait(1)
local frame = script.Parent
local user = game.Players.LocalPlayer

repeat wait() until user.Character 
local char = user.Character

local humanoid = char:WaitForChild("Humanoid")
local anim


-- Function for playing the animations.

function playanim(id)
	if char~=nil and humanoid~=nil then
		local id="rbxassetid://"..tostring(id)
		local oldanim=char:FindFirstChild("LocalAnimation")
		
		if anim~=nil then
			anim:Stop()
		end
		
		if oldanim~=nil then
			if oldanim.AnimationId==id then
				oldanim:Destroy()
				return
			end
			oldanim:Destroy()
		end
		
		while char.Parent ~= game.Workspace do
			char.AncestryChanged:wait()
		end
		
		local animation=Instance.new("Animation",char)
		animation.Name="LocalAnimation"
		animation.AnimationId=id
		anim=humanoid:LoadAnimation(animation)
		anim:Play()
	end
end
```
1 Like

It’s because when the player resets and respawns, the char variable would be referencing nil, since it references the now deleted character. Have a user.CharacterAdded event set up so it updates the char and humanoid (and probably anim)

user.CharacterAdded:Connect(function(character)
    char = character
    humanoid = char:WaitForChild("Humanoid")
    anim = nil
end)

Also

Can be changed to

local char = user.Character or user.CharacterAdded:Wait()
1 Like

wait(0.2)
if user.Character then
–Code here.
end

local frame = script.Parent
local user = game.Players.LocalPlayer

local char = user.Character or user.CharacterAdded:Wait()

user.CharacterAdded:Connect(function(character)
	char = character
	humanoid = char:WaitForChild("Humanoid")
	anim = nil
end)

function playanim(id)
	if char~=nil and humanoid~=nil then
		local id="rbxassetid://"..tostring(id)
		local oldanim=char:FindFirstChild("LocalAnimation")
		
		if anim~=nil then
			anim:Stop()
		end
		
		if oldanim~=nil then
			if oldanim.AnimationId==id then
				oldanim:Destroy()
				return
			end
			oldanim:Destroy()
		end
		
		while char.Parent ~= game.Workspace do
			char.AncestryChanged:wait()
		end
		
		local animation=Instance.new("Animation",char)
		animation.Name="LocalAnimation"
		animation.AnimationId=id
		anim=humanoid:LoadAnimation(animation)
		anim:Play()
	end
end

Hey thanks for the response! So this is the new code using what you suggested and it works… partially. The animations don’t work until the player dies. So when you spawn in the server for the first time, none of the animations won’t play, and there are no displayed errors. But when you reset, everything works perfectly.

I could probably trouble shoot this myself, but have any quick thoughts on what’s going wrong?

You only initalize the character variable,


local char = user.Character or user.CharacterAdded:Wait()

user.CharacterAdded:Connect(function(character)
	char = character
	humanoid = char:WaitForChild("Humanoid")
	anim = nil
end)

this bit should be this


local char = user.Character or user.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local anim

user.CharacterAdded:Connect(function(character)
	char = character
	humanoid = char:WaitForChild("Humanoid")
	anim = nil
end)
3 Likes

Worked like a charm, I really appreciate it!

2 Likes