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