Object stops following player after falling off the map but works fine if the player resets

I want to have a card that the player has favorited follow them around and it all works as intended until the player falls off the map. For some reason this breaks the code, but resetting does not.

The card part is reparented properly and its BodyPosition and BodyGyro part’s values are updated properly even after falling off the map and respawning, but the part’s position does not move, it just sits at where the character died at the bottom of the map.

function StarredClient.Start()
	task.wait(1)

	starredCardPart.Transparency = 1
	starredCardPart.BillboardGui.Title.Visible = false
	starredCardPart.SurfaceGui.cardImage.Visible = false

	local updateLoop = nil

	local function attachToCharacter(character)

		local humanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)
		if not humanoidRootPart then return end

		if updateLoop then
			updateLoop:Disconnect()
		end

		starredCardPart.Parent = character
		starredCardPart.Transparency = 0
		starredCardPart.BillboardGui.Title.Visible = true
		starredCardPart.SurfaceGui.cardImage.Visible = true

		local bGyro = starredCardPart:FindFirstChildOfClass("BodyGyro") or Instance.new("BodyGyro", starredCardPart)
		bGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

		local bPos = starredCardPart:FindFirstChildOfClass("BodyPosition") or Instance.new("BodyPosition", starredCardPart)
		bPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

		updateLoop = game:GetService("RunService").Heartbeat:Connect(function()
			if character and character.Parent and humanoidRootPart then
				bPos.Position = humanoidRootPart.Position + Vector3.new(2, 2, 3)
				bGyro.CFrame = humanoidRootPart.CFrame
			end
		end)
	end

	if player.Character then
		attachToCharacter(player.Character)
	end

	player.CharacterAdded:Connect(function(newCharacter)
		task.wait(1)
		attachToCharacter(newCharacter)
	end)

end
1 Like

Do you create or clone a new starredCardPart on each character respawn?
Because i noticed that you’re parenting this part to the character but when the character respawn it get destroyed, so this part should be destroyed as well.

1 Like

This solved my issue, thanks! I hadn’t thought of that since for some reason I could still see the old card in the map and workspace.

Here’s what I added/changed for anyone that it might help:

player.CharacterAdded:Connect(function(newCharacter)
		starredCardPart = guiElements:WaitForChild("StarredCard"):Clone()
		StarredClient.ChangeStarredCard(starredCard)
		task.wait(1)
		attachToCharacter(newCharacter)
	end)
	
	player.CharacterRemoving:Connect(function()
		starredCardPart:Destroy()
	end)
1 Like