Pet Issue (Destroying problem)

Hey there, I am working on pets for my game and I don’t know why this is happening:
Workspace pic: Screenshot 2021-03-06 130557
My code:

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.Humanoid.Died then
			local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
			if character.Head then 
				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	
	else
		Object:Destroy()
	end	
end

What happens:
robloxapp-20210306-1301248.wmv(4.3 MB)
At first the it just stays there, even though it is supposed to destroy. And then when I jump off the world I get this error

  13:01:56.716  Head is not a valid member of Model "Workspace.MRKYLO20"  -  Server - Script:17
  13:01:56.716  Stack Begin  -  Studio
  13:01:56.716  Script 'Workspace.Pets.StarPet.Script', Line 17  -  Studio - Script:17
  13:01:56.717  Stack End  -  Studio

and it came from the

if character.Head then

I have set multiple points along the code where it should destroy it, but it doesn’t. Does anyone know why in the world this is happening?

This is confusing, are you checking in line 17 if the namevalue is called head?

1 Like

The error in line 17 simply means the script runs while the character’s model is being destroyed.
So when you die to the void your error is “Head is not a valid Member of Model” because your head is gone.

To fix this you simply have to add

if character:FindFirstChild("Head") ~= nil then
if character.Head then

and to fix your pet not deleting. when you said:
"
if NAME.Value ~= nil then
local character = NAME.Value
script.Parent.Head.AlignOrientation.Attachment1 = character:WaitForChild(“Head”).NeckRigAttachment
else
Object:Destroy()
end

"
the pet name is a value so instead of doing if name.value ~= nil do

If Name.Value ~= "" then

1 Like

Maybe do a object:WaitForChild(“Head”) instead?

1 Like

Thank you, that fixed my error, but sadly your second thing did not fix the destroying part, the reason for this is because the value never gets changed when the player dies, so I made added something for when the character dies (the PetDestroy function is all that matters for this)(game.workspace.pets is a folder for the pets)

local Players = game:GetService("Players")
local pets = game.ReplicatedStorage.Pets


local function whichPet(character)
	local player = game.Players:GetPlayerFromCharacter(character)
	local petName = player:WaitForChild("PlayerPet").Value
	local petClone = pets:FindFirstChild(petName):Clone()
	petClone.NameValue.Value = character
	petClone.Parent = workspace.Pets

end

local function DestroyPet(character)
	for i, pet in pairs(game.Workspace.Pets:GetChildren()) do
		if pet.NameValue.Value == character.Name then
			pet.NameValue.Value = nil
		end
	end
end

local function onPlayerAdded(player)
	player.CharacterRemoving:Connect(DestroyPet)
	player.CharacterAdded:Connect(whichPet)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

but the pet script in the topic still didn’t destroy it, so I tried to do it from another script instead of the pet one and just changed this one to

local Players = game:GetService("Players")
local pets = game.ReplicatedStorage.Pets


local function whichPet(character)
	local player = game.Players:GetPlayerFromCharacter(character)
	local petName = player:WaitForChild("PlayerPet").Value
	local petClone = pets:FindFirstChild(petName):Clone()
	petClone.NameValue.Value = character
	petClone.Parent = workspace.Pets

end

local function DestroyPet(character)
	for i, pet in pairs(game.Workspace.Pets:GetChildren()) do
		if pet.NameValue.Value == character.Name then
			pet:Destroy()
		end
	end
end

local function onPlayerAdded(player)
	player.CharacterRemoving:Connect(DestroyPet)
	player.CharacterAdded:Connect(whichPet)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

STILL it wouldn’t do a thing to destroy it with no errors!

I thought I had fixed it from this other topic, but I guess the problem was a little more elaborate than that…