I have a script in my game that essentially clones and parents a custom overhead nametag into the player’s Head upon joining or respawn. The issue is that it’s working for me and other players in every single other game I paste the script into, beside one very specific one. What’s even weirder is that it works for ME in that game, but not anyone else… I’m not sure why. Could anyone provide any insight on this or maybe a solution on how to fix this? Thanks!
Code:
local Players = game:GetService("Players")
local function createDefaultNametag(player, Head)
local groupRank = player:GetRoleInGroup(123456789)
local otg = script.Rank:Clone()
local Frame = otg.Frame
local Name1 = Frame.Name1
local Rank = Frame.Rank
Name1.Text = player.Name
Rank.Text = groupRank
Rank.TextColor = player.TeamColor
otg.Parent = Head
end
local function onPlayerJoin(player)
local updatedNametag
local firstTimeSpawning = true
player.CharacterAdded:Connect(function(Character)
local Head = Character:WaitForChild("Head")
if firstTimeSpawning == true then
firstTimeSpawning = false
createDefaultNametag(player, Head)
else
if updatedNametag == nil then
createDefaultNametag(player, Head)
else
updatedNametag.Parent = Head
end
end
end)
player.CharacterRemoving:Connect(function(oldCharacter)
local Head = oldCharacter:WaitForChild("Head")
local existingNametag = Head:WaitForChild("Rank")
if existingNametag then
updatedNametag = existingNametag:Clone()
end
end)
end
Players.PlayerAdded:Connect(onPlayerJoin)
for _, player in Players:GetPlayers() do
task.spawn(onPlayerJoin, player)
end
Are there any errors that appear in the Developer Console / Output when they join the game? And also, when you mentioned that it doesn’t work for them, is it that both the initial nametag and the “updatedNameTag” doesn’t work for anyone else, or just the updated one is not being given to them upon respawning?
Oh I just realized something I completely overlooked when writing that up the other day; it never calls the createDefaultNametag function until the first time the CharacterAdded event runs for the player.
Here’s a revision that fixes that, but if that doesn’t solve the issue you originally described, let me know:
local Players = game:GetService("Players")
local function createDefaultNametag(player, Head)
local groupRank = player:GetRoleInGroup(123456789)
local otg = script.Rank:Clone()
local Frame = otg.Frame
local Name1 = Frame.Name1
local Rank = Frame.Rank
Name1.Text = player.Name
Rank.Text = groupRank
Rank.TextColor = player.TeamColor
otg.Parent = Head
end
local function onPlayerJoin(player)
local updatedNametag
local firstTimeSpawning = true
local existingCharacter = player.Character
if existingCharacter then
firstTimeSpawning = false
local Head = existingCharacter:WaitForChild("Head")
createDefaultNametag(player, Head)
end
player.CharacterAdded:Connect(function(Character)
local Head = Character:WaitForChild("Head")
if firstTimeSpawning == true then
firstTimeSpawning = false
createDefaultNametag(player, Head)
else
if updatedNametag == nil then
createDefaultNametag(player, Head)
else
updatedNametag.Parent = Head
end
end
end)
player.CharacterRemoving:Connect(function(oldCharacter)
local Head = oldCharacter:WaitForChild("Head")
local existingNametag = Head:WaitForChild("Rank")
if existingNametag then
updatedNametag = existingNametag:Clone()
end
end)
end
Players.PlayerAdded:Connect(onPlayerJoin)
for _, player in Players:GetPlayers() do
task.spawn(onPlayerJoin, player)
end
Thanks for getting back to me! I tried this new script, and it still doesn’t seem to be working. No errors seem to be popping up; I’m still having the same issue as I described originally in this post!
One more idea I have at the moment would be to split up script.Rank:Clone() into two separate variables in case that happens to be causing an issue where it’s not really creating another default nametag after the first player joins.
One way you might be able to test if that’s the case before making this change would be to have someone else join the game first, then for you to join. If suddenly they were given the nametag but you weren’t, that would likely indicate that that was the issue.
Example Revision
local Players = game:GetService("Players")
local originalRank = script.Rank
local function createDefaultNametag(player, Head)
local groupRank = player:GetRoleInGroup(123456789)
local otg = originalRank:Clone()
local Frame = otg.Frame
local Name1 = Frame.Name1
local Rank = Frame.Rank
Name1.Text = player.Name
Rank.Text = groupRank
Rank.TextColor = player.TeamColor
otg.Parent = Head
end
local function onPlayerJoin(player)
local updatedNametag
local firstTimeSpawning = true
local existingCharacter = player.Character
if existingCharacter then
firstTimeSpawning = false
local Head = existingCharacter:WaitForChild("Head")
createDefaultNametag(player, Head)
end
player.CharacterAdded:Connect(function(Character)
local Head = Character:WaitForChild("Head")
if firstTimeSpawning == true then
firstTimeSpawning = false
createDefaultNametag(player, Head)
else
if updatedNametag == nil then
createDefaultNametag(player, Head)
else
updatedNametag.Parent = Head
end
end
end)
player.CharacterRemoving:Connect(function(oldCharacter)
local Head = oldCharacter:WaitForChild("Head")
local existingNametag = Head:WaitForChild("Rank")
if existingNametag then
updatedNametag = existingNametag:Clone()
end
end)
end
Players.PlayerAdded:Connect(onPlayerJoin)
for _, player in Players:GetPlayers() do
task.spawn(onPlayerJoin, player)
end