NameTag Script Issue

The nametag itself works perfectly fine, but when a player tries to go into a different team since they joined that group (the team is linked to the group, and shows the rank they are in), the nametag doesn’t change at all.

Nametag would change color based on what team they are in, but doesn’t do that at all.

If anyone could help, that would be awesome.

local groupID = 32989980
local HQ = 33923356
local ESFOZA = 33923463
local RG = 33534442
local TERDAC = 33923286
local ZAAF = 33923232
local ZAMPC = 33923505

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local ranks = {
	[255] = {Name = "[Z] Director", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[254] = {Name = "[HDEV] Head Developer", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[253] = {Name = "[DEV] Developer", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[252] = {Name = "[CZA] Chairman of ZA", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[251] = {Name = "[AA] Arch Admiral", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[250] = {Name = "[A] Admiral", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[24] = {Name = "[CO] Commander", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[22] = {Name = "[Z10] General", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[21] = {Name = "[Z9] Lieutenant General", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[20] = {Name = "[Z8] Major General", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[19] = {Name = "[Z7] Brigadier General", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[18] = {Name = "[Z6] Colonel", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[17] = {Name = "[Z5] Lieutenant Colonel", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[16] = {Name = "[Z4] Major", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[15] = {Name = "[Z3] Captain", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[14] = {Name = "[Z2] First Lieutenant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[13] = {Name = "[Z1] Second Lieutenant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[11] = {Name = "[R11] Command Sergeant Major", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(24, 255, 217)},
	[10] = {Name = "[R10] Sergeant Major", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[9] = {Name = "[R9] First Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[8] = {Name = "[R8] Master Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[7] = {Name = "[R7] Sergeant First Class", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[6] = {Name = "[R6] Staff Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[5] = {Name = "[R5] Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[4] = {Name = "[R4] Corporal", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)},
	[3] = {Name = "[R3] Specialist", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)},
	[2] = {Name = "[R2] Private First Class", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)},
	[1] = {Name = "[R1] Private [Combat Training]", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)}
}

local divisions = {
	[ESFOZA] = {Prefix = "[ESFOZA]", Image = "rbxassetid://16514196686", Color = Color3.fromRGB(190, 180, 172)},
	[TERDAC] = {Prefix = "[TERDAC]", Image = "rbxassetid://16513878352", Color = Color3.fromRGB(255, 48, 6)},
	[RG] = {Prefix = "[RG]", Image = "rbxassetid://16433575026", Color = Color3.fromRGB(255, 162, 0)},
	[ZAMPC] = {Prefix = "[ZAMPC]", Image = "rbxassetid://16514272731", Color = Color3.fromRGB(255, 98, 25)},
	[ZAAF] = {Prefix = "[ZAAF]", Image = "rbxassetid://16513766481", Color = Color3.fromRGB(58, 252, 255)},
	[HQ] = {Prefix = "[HQ]", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 26)}
}

local function SetupOverheadUI(player, character)
	local head = character:WaitForChild("HumanoidRootPart")
	local overheadUI = script.OverheadGUI:Clone()

	overheadUI.Parent = head
	overheadUI.PlrName.Text = player.Name
	overheadUI.PlrRank.Text = player:GetRoleInGroup(groupID)

	local rank = ranks[player:GetRankInGroup(groupID)]
	local division = overheadUI.Divisions

	if rank then
		division.DivName.Text = rank.Name
		division.DivImage.Image = rank.Image
		division.DivDesign.BackgroundColor3 = rank.Color
	else
		local inGroup = false
		for group, info in pairs(divisions) do
			if player:IsInGroup(group) then
				division.DivName.Text = info.Prefix .. " " .. player:GetRoleInGroup(group)
				division.DivImage.Image = info.Image
				division.DivDesign.BackgroundColor3 = info.Color
				inGroup = true
				break
			end
		end
		if not inGroup then
			if player:IsInGroup(groupID) then
				division.DivName.Text = "Divisionless"
				division.DivImage.Image = "rbxassetid://17365601663"
				division.DivDesign.BackgroundColor3 = Color3.fromRGB(152, 166, 177)
			else
				division.DivName.Text = "Civilian"
				division.DivImage.Image = "rbxassetid://17365601663"
				division.DivDesign.BackgroundColor3 = Color3.fromRGB(170, 0, 255)
			end
		end
	end

	if UIS.TouchEnabled and not UIS.KeyboardEnabled then
		overheadUI.Icons.Mobile.Visible = true
	elseif UIS.KeyboardEnabled and not UIS.TouchEnabled then
		overheadUI.Icons.PC.Visible = true
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		SetupOverheadUI(player, character)
	end)
end)

Well you aren’t detecting when a player changes a team, so why would they? Change the playeradded code at the bottom to this, where it detects when a player changes their team. This also means when refreshing the nametag you will have to destroy the previous one there.

game.Players.PlayerAdded:Connect(function(player)
    -- Check when the player respawns and refresh his nametag.
    player.CharacterAdded:Connect(function(character)
        SetupOverheadUI(player, character)
    end)
    
    -- Check when player changes teams and refresh his nametag.
    player:GetPropertyChangedSignal("Team"):Connect(function()
        SetupOverheadUI(player, character)
    end)
end)

You can destroy it by getting the character, then the head, then the UI.

function SetupOverheadUI(player, character)
    -- Get the users humanoid root part and destroy the previous UI.
    local HumanoidRootPart =  character:FindFirstChild("HumanoidRootPart")
    if HumanoidRootPart:FindFirstChild("OverheadGUI") then
         HumanoidRootPart:FindFirstChild("OverheadGUI"):Destroy()
    end
    -- Rest of adding code, don't change it.
end

Also don’t use WaitForChild in Server Scripts, use FindFirstChild or dot notation.

Thank you for the adjustments. Sorry for the late answer, I have been on LOA. However, where would I put the ‘function SetupOverheadUI’ script at?

And do I change all ‘WaitForChild’ scripts to ‘FindFirstChild’ or ‘dot notation’?

In your main script that already has the SetupOverheadUI function, at the bottom you have a PlayerAdded function, remove that. Then copy the first code I made and paste it there. Then go to your SetupOverheadUI function in your code. In the 2nd code box I made, copy lines 3-6, basically the main lines of code. Go to the top of your SetupOverheadUI and paste in the code you copied.

Script is still giving me errors and red outlines for the character and SetupOverheadUI.

Then it’s the rest of your script, can you tell me the errors?

Sorry if I didn’t do the code correctly. Not really good at it. Let me know how to fix the script so it works perfectly fine.

local groupID = 32989980
local HQ = 33923356
local ESFOZA = 33923463
local RG = 33534442
local TERDAC = 33923286
local ZAAF = 33923232
local ZAMPC = 33923505

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local ranks = {
	[255] = {Name = "[Z] Director", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[254] = {Name = "[HDEV] Head Developer", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[253] = {Name = "[DEV] Developer", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[252] = {Name = "[CZA] Chairman of ZA", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[251] = {Name = "[AA] Arch Admiral", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[250] = {Name = "[A] Admiral", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[24] = {Name = "[CO] Commander", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[22] = {Name = "[Z10] General", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[21] = {Name = "[Z9] Lieutenant General", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 43)},
	[20] = {Name = "[Z8] Major General", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[19] = {Name = "[Z7] Brigadier General", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[18] = {Name = "[Z6] Colonel", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[17] = {Name = "[Z5] Lieutenant Colonel", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[16] = {Name = "[Z4] Major", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[15] = {Name = "[Z3] Captain", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[14] = {Name = "[Z2] First Lieutenant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[13] = {Name = "[Z1] Second Lieutenant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(255, 167, 43)},
	[11] = {Name = "[R11] Command Sergeant Major", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(24, 255, 217)},
	[10] = {Name = "[R10] Sergeant Major", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[9] = {Name = "[R9] First Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[8] = {Name = "[R8] Master Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[7] = {Name = "[R7] Sergeant First Class", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[6] = {Name = "[R6] Staff Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[5] = {Name = "[R5] Sergeant", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(49, 255, 248)},
	[4] = {Name = "[R4] Corporal", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)},
	[3] = {Name = "[R3] Specialist", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)},
	[2] = {Name = "[R2] Private First Class", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)},
	[1] = {Name = "[R1] Private [Combat Training]", Image = "rbxassetid://17365601663", Color = Color3.fromRGB(125, 125, 125)}
}

local divisions = {
	[ESFOZA] = {Prefix = "[ESFOZA]", Image = "rbxassetid://16514196686", Color = Color3.fromRGB(190, 180, 172)},
	[TERDAC] = {Prefix = "[TERDAC]", Image = "rbxassetid://16513878352", Color = Color3.fromRGB(255, 48, 6)},
	[RG] = {Prefix = "[RG]", Image = "rbxassetid://16433575026", Color = Color3.fromRGB(255, 162, 0)},
	[ZAMPC] = {Prefix = "[ZAMPC]", Image = "rbxassetid://16514272731", Color = Color3.fromRGB(255, 98, 25)},
	[ZAAF] = {Prefix = "[ZAAF]", Image = "rbxassetid://16513766481", Color = Color3.fromRGB(58, 252, 255)},
	[HQ] = {Prefix = "[HQ]", Image = "rbxassetid://16514021179", Color = Color3.fromRGB(255, 167, 26)}
}

local function SetupOverheadUI(player, character)
	local head = character:WaitForChild("HumanoidRootPart")
	local overheadUI = script.OverheadGUI:Clone()

	overheadUI.Parent = head
	overheadUI.PlrName.Text = player.Name
	overheadUI.PlrRank.Text = player:GetRoleInGroup(groupID)

	local rank = ranks[player:GetRankInGroup(groupID)]
	local division = overheadUI.Divisions

	if rank then
		division.DivName.Text = rank.Name
		division.DivImage.Image = rank.Image
		division.DivDesign.BackgroundColor3 = rank.Color
	else
		local inGroup = false
		for group, info in pairs(divisions) do
			if player:IsInGroup(group) then
				division.DivName.Text = info.Prefix .. " " .. player:GetRoleInGroup(group)
				division.DivImage.Image = info.Image
				division.DivDesign.BackgroundColor3 = info.Color
				inGroup = true
				break
			end
		end
		if not inGroup then
			if player:IsInGroup(groupID) then
				division.DivName.Text = "Divisionless"
				division.DivImage.Image = "rbxassetid://17365601663"
				division.DivDesign.BackgroundColor3 = Color3.fromRGB(152, 166, 177)
			else
				division.DivName.Text = "Civilian"
				division.DivImage.Image = "rbxassetid://17365601663"
				division.DivDesign.BackgroundColor3 = Color3.fromRGB(170, 0, 255)
			end
		end
	end

	if UIS.TouchEnabled and not UIS.KeyboardEnabled then
		overheadUI.Icons.Mobile.Visible = true
	elseif UIS.KeyboardEnabled and not UIS.TouchEnabled then
		overheadUI.Icons.PC.Visible = true
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		SetupOverheadUI(player, character)
	end)
end)

**player**:GetPropertyChangedSignal("Team"):Connect(function()
	SetupOverheadUI(**player, character**)
end)

The words with “**” by them are the underlined problems.

change that to this

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		SetupOverheadUI(player, character)

	end)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		SetupOverheadUI(player, player.Character)
	end)
end)


1 Like

Still shows “character” being underlined. Its the code on line 6.

image_2024-08-04_211036596

Try now I edited it. I didn’t see your error before

The error went away, should I test the script?

Of course you should test the script

Script did not work. I have no idea what is causing this issue.
Screenshot 2024-08-04 212825

Nametag should change based off of which player joins which team, since the group bind is scripted to the team on the script.

Let me know if there is a fix.

I don’t really want to go through the whole thing. But look at this, where in the WORLD are you getting group from?

local divisions = {
	[ESFOZA] = {Prefix = "[ESFOZA]", Image = "rbxassetid://16514196686", Color = Color3.fromRGB(190, 180, 172)},

To me, I don’t see a group ID anywhere! Like there are things like this that you just need to look for. Change the divisions to actually HAVE the group id.

local divisions = {
	[GROUP ID HERE] = {Prefix = "[ESFOZA]", Image = "rbxassetid://16514196686", Color = Color3.fromRGB(190, 180, 172)},