Pet Problem (Won't Destroy)

Hi there! I am having a problem with my pet not destroying when I reset my character, and it doesn’t make sense…

local TweenService = game:GetService("TweenService")
local Object = script.Parent
local NAME = Object.NameValue
wait(3)
if NAME.Value ~= nil then
	local character = NAME.Value
	script.Parent.Head.AlignOrientation.Attachment1 = character:WaitForChild("Head").NeckRigAttachment
else
	Object:Destroy()
end
while true do
	wait()
	local character = NAME.Value
	if NAME.Value ~= nil then
		if character:FindFirstChild("Head") then
			local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		 	
			local tween = TweenService:Create(Object.Head, tweenInfo, {CFrame = character.Head.NeckRigAttachment.CFrame:ToWorldSpace(character.Head.CFrame * CFrame.new(2, 1, 1))} )
		 
			tween:Play()
		else
			Object:Destroy()
		end	
	else
		Object:Destroy()
	end	
end

In the while loop I have two spots where if the character is destroyed it will destroy itself, the second just for extra precaution. Could someone tell me what I am missing? It just won’t destroy when the character dies.

1 Like

I believe that the character and the Head still exist when the player dies. Try instead checking if the humanoid.Health > 0

I heard that isn’t the best way to go about it because your health still heals after you die. There’s Humanoid.Died for that

Or plr.CharacterRemoving

1 Like

Thanks! I didn’t think of that, though where in the world did the old character go if it wasn’t destroyed…

I am pretty sure that the character stays in the workspace until the new character is spawned. Then the original character is destroyed. So there is always a character in the workspace.