I have a script that adds a BillboardGui to the player’s head that is not working about half the time. It attaches to the player’s head as it should but sometimes it will mysteriously get destroyed. I’m confused about how this is happening and how to fix it. Please help me figure out how to fix this, thanks.
local Character = player.Character or player.CharacterAdded:Wait()
print(Character) -- Always prints the correct character
jumpsDataStore:OnUpdate(jumpsUpdate)
local overhead = game.ReplicatedStorage:WaitForChild("OverheadUI"):Clone()
overhead.Parent = Character.Head
print(overhead.Parent) -- Always prints "Head" as it should
overhead.TextLabel.Text = "Jumps: "..jumps.Value
print(overhead.TextLabel.Text)
task.wait(1)
print(overhead.Parent) -- Prints as nil sometimes (somehow destroyed?)
I havent seen that happen before
maybe it’s something like the code parenting the billboardgui before the character is in workspace (but after it’s added to the player), then when setting up the appearance, the billboard gets removed?
try checking if the character’s parent is in workspace first, or putting the billboardgui in PlayerGui and setting the adornee to the head
I didn’t even think of this, thank you so much! I was confused about this for so long and it ended up being just 1 simple line fix. game.Workspace:WaitForChild(Character.Name)
Edit: Using adornee also works and I did that instead
local nametag = game.ReplicatedStorage.NameTag
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local head = char:FindFirstChild("Head")
repeat task.wait() until head ~= nil
local newNametag = nametag:Clone()
newNametag.Parent = head
end)
end)