How to make a part get removed when you die

I made a pet script where a part aka the pet will follow you when you spawn. I am trying to make it where when you die the part aka the pet gets removed. Code:

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
	local pet = game.ServerStorage.Part
	pet.Parent = game.Workspace
	pet.Anchored = false
	pet.CanCollide = false
	local BP = Instance.new("BodyPosition",pet)
	
	while wait() do
		BP.Position = char:FindFirstChild("Head").Position + Vector3.new(3,2,0)
	end
	
end)

end)

So i’m assuming here the problem is that the pet is not being removed. Make sure to include the problem in your post!

The problem and solution:
It seems you aren’t destroying the pet when the player dies. You can detect when the player dies via the humanoid.died event:

--Put this at the inside your character added connection
char.Humanoid.Died:Connect(function()
    pet:Destroy()
end)

A little thing that you should change!
When you run local pet = game.ServerStorage.Part you are referencing the actual part inside ServerStorage. That means when you run pet.Parent = game.Workspace, the pet will be moved from ServerStorage to the workspace and any other players that will try running the local pet = game.ServerStorage.Part inside this script will error because the pet isn’t there any more! Change your local pet = game.ServerStorage.Part to local pet = game.ServerStorage.Part:Clone(). This will clone your pet so the original pet isn’t manipulated.

1 Like

1 thing you might wanna do is clone the pet before you parent it to the workspace so that every player that joins gets one, then name the pet something like "player.Name’s pet so it’s easily identifiable

For removing it, do a humanoid.Died event and remove it from there

char.Humanoid.Died:Connect(function()
     local pet = workspace:FindFirstChild(player.Name .. "'s pet")

     if pet then pet:Destroy() end
end)
1 Like

Oh I get it now, it basically checks the name to make sure its the pet

Yes, to make sure you are removing the correct pet

Ok I understand it its basically adding an event. Thank you for the info.

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
	local pet = game.ServerStorage.Part:Clone()
	pet.Parent = game.Workspace
	pet.Anchored = false
	pet.CanCollide = false
	local BP = Instance.new("BodyPosition", pet)

	while wait() do 
		BP.Position = char:FindFirstChild("Head").Position + Vector3.new(3,2,0)
	end
	
	char.Humanoid:Connect(function()
		pet:Destroy()
	end)
end)

end)

That is the updated code but it still does not remove the part aka the pet when you die.

There are a couple of problems here that are preventing your code from working. First, your while wait() loop is consistently executing and therefore, the event below is never connected. You can fix this by wrapping it in spawn() or using coroutines. Here’s how it’d look with spawn():

spawn(function()
	while wait() do
		BP.Position = char:FindFirstChild("Head").Position + Vector3.new(3,2,0)
	end
end

Second, your event connection isn’t set up correctly; you’re connecting to the Humanoid which isn’t an event and won’t fire. You should use char.Humanoid.Died:Connect() instead.

Third, your while wait() loop will run indefinitely since there isn’t anything to stop it once the player dies. I’d recommend defining a separate variable and then setting it to false once the player dies. Sort of like this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char) 
		local Check = true -- Here's the variable

		spawn(function)
			while Check do
				BP.Position = char:FindFirstChild("Head").Position + Vector3.new(3,2,0)
				wait()
			end
		end

		char.Humanoid.Died:Connect(function)
			Check = false --This sets the variable to false so the loop stops
			pet:Destroy()
		end
	end
end
1 Like

??

this isnt an event. Try

char.Humanoid.Died:connect(function()
pet:Destroy()
end)

also, I recently made a module that could help with this:

essentially with the module you can bind the pet to your character so that when the character is removed, the pet will be also. Only 1 line of code needed!