Custom Nicknames not working with custom overhead

Im attempting to set nicknames based on player name, however its not working for some reason anymore?

function onPlayerRespawned(newPlayer)
	wait(2)
		local gui = script.RankName:Clone()
		local rank = gui.Frame.Rank
		local plrname = gui.Frame.plrName
		local B1 = gui.Frame.Bracket
		local B2 = gui.Frame.Bracket1
		gui.Parent = newPlayer.Character.Head
		plrname.TextColor3 = newPlayer.TeamColor.Color
		rank.TextColor3 = newPlayer.TeamColor.Color
		B1.TextColor3 = newPlayer.TeamColor.Color
		B2.TextColor3 = newPlayer.TeamColor.Color

	newPlayer.Character.Humanoid.NameDisplayDistance = 0
	local Council = {
	['O5-1 "The Blackjack"'] = "DanielVulpes",
	['O5-2 "The Nazarine"'] = "MazarineVenturaIX",
	['O5-3'] = "N/A",
	['O5-4 "Father of Lies"'] = "AzraelVonAurach",
	['O5-5 "The Purdig"'] = "Purdig",
	['O5-6 "The Alpha"'] = "Christophergaming3",
	['O5-7 "The Engineer"'] = "BrokenVondar",
	['O5-8 "Dr. Vulpes"'] = "OliverVulpes",
	['O5-9'] = "N/A",
	['O5-10'] = "N/A",
	['O5-11'] = "N/A",
	['O5 Head'] = "N/A",
	['Vice Administrator'] = "N/A",
	['The Administrator'] = "N/A",
	}
	
	for o5number,user in pairs(Council) do
		if newPlayer.Name == user then
			plrname.Text = o5number
		else
			plrname.Text = newPlayer.Name
		end
	end
			
		if newPlayer.Team.Name == "Engineering & Technical" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051713) .. "")
		elseif newPlayer.Team.Name == "Ethics Committee" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051692) .. "")
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051715) .. "")		
		elseif newPlayer.Team.Name == "Intelligence Command Agency" then
		plrname.Text = "████████████████"
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5548972) .. "")		
		elseif newPlayer.Team.Name == "Department of External Affairs" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5056299) .. "")		
		elseif newPlayer.Team.Name == "Medical Department" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051683) .. "")		
		elseif newPlayer.Team.Name == "Mobile Task Force" then
		plrname.Text = ("Operative-" .. newPlayer.UserId .. "")
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051629) .. "")		
		elseif newPlayer.Team.Name == "Scientific Department" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051665) .. "")		
		elseif newPlayer.Team.Name == "Security Department" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5051582) .. "")		
		elseif newPlayer.Team.Name == "Manufacturing Department" then
		rank.Text = ("" .. newPlayer:GetRoleInGroup(5211618) .. "")		
		elseif newPlayer.Team.Name == "Class D" and newPlayer:GetRankInGroup(4885004) <= 1 then
		rank.Text = ("D-" .. newPlayer.UserId .. "")		
		else
		rank.Text = ("" .. newPlayer:GetRoleInGroup(4885004) .. "")
		end

end

function onPlayerEntered(newPlayer)
	newPlayer.Changed:connect(function (property)
		if (property == "Character") then
			onPlayerRespawned(newPlayer)
		end
	end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

specifically this block of code:

	newPlayer.Character.Humanoid.NameDisplayDistance = 0
	local Council = {
	['O5-1 "The Blackjack"'] = "DanielVulpes",
	['O5-2 "The Nazarine"'] = "MazarineVenturaIX",
	['O5-3'] = "N/A",
	['O5-4 "Father of Lies"'] = "AzraelVonAurach",
	['O5-5 "The Purdig"'] = "Purdig",
	['O5-6 "The Alpha"'] = "Christophergaming3",
	['O5-7 "The Engineer"'] = "BrokenVondar",
	['O5-8 "Dr. Vulpes"'] = "OliverVulpes",
	['O5-9'] = "N/A",
	['O5-10'] = "N/A",
	['O5-11'] = "N/A",
	['O5 Head'] = "N/A",
	['Vice Administrator'] = "N/A",
	['The Administrator'] = "N/A",
	}
	
	for o5number,user in pairs(Council) do
		if newPlayer.Name == user then
			plrname.Text = o5number
		else
			plrname.Text = newPlayer.Name
		end
	end

Thank you in advance. There are no errors in the script.

It looks like the loop itself has a problem here. Even if you find a match, you’ll move on to the next step - where if it doesn’t match, you’ll reset the text to the player’s name. So in theory, the only valid match would be against the last thing that it checks.

Instead, what I’d do is break out whenever you find a match - then you can set the text like you want. Specifically, change the loop to this:

    plrname.Text = newPlayer.Name -- In case we end up with no match
	for o5number,user in pairs(Council) do
		if newPlayer.Name == user then
			plrname.Text = o5number
            break -- Exit when we have a match
        end
	end

It’s worth mentioning that you would have a much easier time if you reversed the keys and values of your dictionary because then you could perform a direct lookup on whether someone should be given a name or not. You should also be working with UserIds not names - names can change, UserIds are permanent.

A loop is wholly unnecessary in itself here. If there are suitable alternatives where a loop wouldn’t be necessary, use them. You simply need to change how you’re doing things and you would have an easier time trying to accomplish this.

There’s a lot of other pick-ats in terms of your code that should be resolved. It’s rather messy and performing work where not necessary. You are performing work where not necessary. For example, you’re generating that table per player entering the server over using a constant upvalue.

I’m going to keep it to usernames but I would hope you change to UserIds in the future. Here’s a code segment redone which uses your work as a basis.

-- GetService is the canonical way to fetch a service
local Players = game:GetService("Players")

local COUNCIL = {
    ["DanielVulpes"] = "O5-1 \"The Blackjack\"",
    ["MazarineVenturaIX"] = "O5-2 \"The Nazarene\"",
    ["AzraelVonAurach"] = "O5-4 \"The Father of Lies\"",
    ["Purdig"] = "O5-5 \"The Purdig\"",
    ["Christophergaming3"] = "O5-6 \"The Alpha\"",
    ["BrokenVondar"] = "O5-7 \"The Engineer\"",
    ["OliverVulpes"] = "O5-8 \"Dr. Vulpes\"",
}

local DEPARTMENTS = {
    -- Format: GroupId, NameOverrideProcedure
    ["Engineering & Technical"] = {5051713, nil},
    -- In your original code, it also checked IA for EC?
    ["Ethics Committee"] = {5051692, nil},
    ["Intelligence Command Agency"] = {5548972, function()
        return "████████████████"
    end},
    ["Department of External Affairs"] = {5056299, nil},
    ["Medical Department"] = {5051683, nil},
    ["Mobile Task Force"] = {5051629, function(player)
        return "Operative-" .. player.UserId
    end},
    ["Scientific Department"] = {5051665, nil},
    ["Security Department"] = {5051582, nil},
    ["Manufacturing Department"] = {5211618, nil},
    ["Class D"] = {4885004, function(player)
        return "D-" .. player.UserId
    end},
}

-- Make your functions local!
local function addTag(player, character)
    local gui = script.RankName:Clone()
    local plrname = gui.Frame.plrName
    local rank = gui.Frame.Rank
    local B1 = gui.Frame.Bracket
    local B2 = gui.Frame.Bracket1

    -- Use variables to avoid redundancy
    local teamColor = player.Team.TeamColor

    -- Apply defaults
    plrname.Text = player.Name
    rank.Text = player:GetRoleInGroup(4885004)

    plrname.TextColor3 = teamColor
    rank.TextColor3 = teamColor
    B1.TextColor3 = teamColor
    B2.TextColor3 = teamColor

    -- Use StarterPlayer to set NameDisplayDistance to 0, don't do it here

    -- Apply O5 Council renames
    local councilTitle = COUNCIL[player.Name]
    if councilTitle then
        plrName.Text = councilTitle
    end

    -- Apply departments (better than a chain of elses)
    local teamData = DEPARTMENTS[player.Team.Name]
    if teamData then
        local groupId = teamData[1]
        local nameProcedure = teamData[2]

        rank.Text = player:GetRoleInGroup(groupId)
        if nameProcedure then
            plrname.Text = nameProcedure(player)
        end
    end

    -- Parent ONLY after setting everything
    gui.Parent = character.Head
end

local function onPlayerAdded(player)
    -- Use the proper canonical event, not Changed.
    player.CharacterAdded:Connect(function (character)
        -- Call it in an anon function for convenience's sake
        addTitle(player, character)
    end)

    if player.Character then
        -- Run in case they first enter
        addTitle(player, character)
    end
end

Players.PlayerAdded:Connect(onPlayerAdded)
1 Like