How can I improve my above-head name tag code?

I don’t know if I should’ve posted this here, or in Scripting Support. Regardless, I’d like some tips for improving my above-head name tag UI code. While this does work, if multiple people spawn or join at the same time, the code can assign people the same rank.

.rbxl File

CodeReview.rbxl (20.7 KB)

Code + Hierarchy
Code
local billboard = script.BillboardGui



game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(char)
		
		
		local GR = Player:GetRankInGroup(5290077)
		if GR == 15 then RankAbrv = "PENDING"
		elseif GR == 30 then Player:kick ("You are suspended.")
		elseif GR == 45 then RankAbrv = "SUSPICOUS"
		elseif GR == 60 then Player:ban ("Exploiter noob")
		elseif GR == 75 then RankAbrv = "PLR"
		elseif GR == 90 then RankAbrv = "ADNT"
		elseif GR == 105 then RankAbrv = "TM"
		elseif GR == 120 then RankAbrv = "KNTC"
		elseif GR == 135 then RankAbrv = "ADV"
		elseif GR == 150 then RankAbrv = "ODR"
		elseif GR == 165 then RankAbrv = "DEV"
		elseif GR == 180 then RankAbrv = "ALLY"
		elseif GR == 195 then RankAbrv = "INTL"
		elseif GR == 210 then RankAbrv = "BLZ"
		elseif GR == 225 then RankAbrv = "MRN"
		elseif GR == 240 then Player:ban ("Exploiter noob")
		elseif GR == 250 then RankAbrv = "CNCL"
		elseif GR == 252 then RankAbrv = "CMDR"
		elseif GR == 255 then RankAbrv = "HDR"
		elseif GR == 0 then RankAbrv = "GUEST"
		elseif Player.name == "AFuturisticFox" then RankAbrv = "ALLY"
		elseif Player.name == "MainDragon1971" then RankAbrv = "ALLY"
			


		end



		
		wait(0.1)
		print(RankAbrv)
		
			local newgui = billboard:Clone()
			local Usrnm = Player.name
			newgui.Parent = char.Head
			newgui.TextLabel.TextTransparency = 0
			newgui.TextLabel.Text = "["..RankAbrv.."]".." "..Usrnm			

	end)
end)

Hierarchy

image

I recently made a topic about a rainbow nametag maker. I tried applying the same concepts I learned from that, but I never figured out how to apply it.

Thanks!

1 Like

You can simplify all your elseifs with a dictionary.

local Ranks = {
    [15] = "PENDING",
    [45] = "SUSPICIOUS",
    [75] = "PLR"
    -- etc
}

But you’ll still need the elseifs for the special cases.

if Ranks[GR] then
    RankAbrv = Ranks[GR]
elseif GR == 30 then
    Player:kick("You are suspended.")
elseif GR == 60 then
    Player:ban("Exploiter noob")
-- etc
end
3 Likes

Player:ban isn’t a function. You’ll have to write your own using data stores. If a player’s user ID as a key has true as a value in a Banned data store when they join then kick them from the game. When you ban them, set their user ID as a key to true in the data store and kick them from the game.

The value doesn’t have to be true; it could be the string reason that they were banned. You could then use that reason when kicking them.

2 Likes

This is being done from the server. Clients can’t spoof either names or UserIds in any capacity. The effect is only going to occur on their own end, the server will not receive the change and continue to evaluate as expected. For the context of this code though, UserId is inappropriate to use.

You are correct in saying that player-based special function stuff should be conducted with UserIds as opposed to usernames. UserIds are permanently associated with an account, usernames aren’t and can be changed.

If you’re lazy like me, you can feed a username to GetUserIdFromNameAsync to determine what UserId the given name is associated with. Works with previous names as well. Counteracting function is GetNameFromUserIdAsync, which takes a UserId and returns the user’s current username.

4 Likes