Objective: I’m looking to clone a BillboardGui from ReplicatedStorage and set the clone’s parent as a certain player’s head.
Issue: Somehow my script doesn’t seem to work with all the selected player’s name (works 1/2). I was able to conclude that the if statement runs, but there is no BillboardGui under the player’s head.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local nametag = ReplicatedStorage.nametag
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if(player.Name == "Player1") then
local head = char.Head
local nametagClone = nametag:Clone()
nametagClone.Parent = head
nametagClone.TextLabel.Text = "Owner"
end
if(player.Name == "Player2") then
local head = char.Head
local nametagClone = nametag:Clone()
nametagClone.Parent = head
nametagClone.TextLabel.Text = "Owner"
end
end)
end)
Make a table insert all the players that you want.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local nametag = ReplicatedStorage.nametag
local players = {
["Player1"] = {
Rank = "Owner"
},
["Player2"] = {
Rank = "Owner"
}
}
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local head = char:WaitForChild("Head") or char:FindFirstChild("Head")
if players[player.Name] then
local nametagClone = nametag:Clone()
nametagClone.Parent = head
nametagClone.TextLabel.Text = players[player.Name].Rank
end
end)
end)
local head = char.Head
local newNametag = nametag:Clone()
newNametag.Parent = head
newNametag.Adornee = head
print(newNametag.Adornee.Name)
print(newNametag.Parent)
print(newNametag.Parent.Parent)
newNametag.TextLabel.Text = "hello"
The output is as expected:
22:33:44.424 Head - Server - Nametags:12
22:33:44.424 Head - Server - Nametags:13
22:33:44.424 Debrikalis - Server - Nametags:14
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local nametag = ReplicatedStorage.nametag
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local head = char.Head
local newNametag = nametag:Clone()
newNametag.Parent = head
newNametag.Adornee = head
print(newNametag.Adornee.Name)
print(newNametag.Parent)
print(newNametag.Parent.Parent)
newNametag.TextLabel.Text = "hello"
end)
end)
Then it seems like the parent is being set to nil, meaning the character’s head hasn’t been loaded yet.
Instead of char.Head, try char:WaitForChild(“Head”)