My problem is, for some reason, this script doesn’t work for all people, for example, my main account never has a nametag above their head, and when using the “!role” command, nothing happens.
My question is, why is this happening, and how can I fix this so that all players get a name-tag above their head?
Name tag script:
local GroupId = 6708836
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Head = Character:WaitForChild("Head")
local NameTag = NameTagTemplate:Clone()
NameTag.Parent = Head
NameTag.NameText.Text = Player.Name
NameTag.RankText.Text = Player:GetRoleInGroup(GroupId)
Character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
end)
end)
!role command script:
local MinimumRankToUseCommand = 8
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
end)
Player.Chatted:Connect(function(Message)
local SplitMessage = Message:split(" ")
if SplitMessage[1] == "!role" and Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
local NameOfPlayerToWarn = SplitMessage[2]
local PlayerToChange = game.Players:FindFirstChild(NameOfPlayerToWarn)
local Reason = Message:split(NameOfPlayerToWarn)[2]
local Head = PlayerToChange.Character:WaitForChild("Head")
local WarningGUI = PlayerToChange.Character.Head.NameTag.RankText
WarningGUI.Text = Reason
end
end)
end)```
I did some more investigation and found out that the !role command script works fine, and it’s the name tag script that isn’t working. Could anyone help me figure out what’s wrong with it?
There’s a chance characters will load before the .CharacterAdded event fires, causing them to be ignored by the current script. To counter this issues, the script must assume the character may be already loaded:
local NameTagTemplate = template.path.here
local GroupId = 6708836
function PlayerAdded(player)
local function CharacterAdded(char)
--adding a time limit of 5, to avoid infinity yield
local Head = char:WaitForChild("Head", 5)
if not Head then warn("Head wasn't found") return end
local NameTag = NameTagTemplate:Clone()
NameTag.Parent = Head
NameTag.NameText.Text = player.Name
NameTag.RankText.Text = player:GetRoleInGroup(GroupId)
char.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
end
--if it errors the first time, it wont stop the entire script
pcall(function()
CharacterAdded(player.Character or player.CharacterAdded:Wait())
end)
player.CharacterAdded:Connect(CharacterAdded)
end
for _, player in pairs(Players:GetPlayers()) do
PlayerAdded(player)
end
Players.PlayerAdded:Connect(PlayerAdded)
and to avoid all this messy code, you can take advantage of StarterCharacterScripts which will ensure the script runs when a character loads:
--Script inside StarterCharacterScripts
local Players = game:GetService("Players")
local NameTagTemplate = template.path.here
local GroupId = 6708836
local Character = script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
if not Player then return end --ignore npcs
local Head = Character:WaitForChild("Head")
local NameTag = NameTagTemplate:Clone()
NameTag.Parent = Head
NameTag.NameText.Text = Player.Name
NameTag.RankText.Text = Player:GetRoleInGroup(GroupId)
Character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None