Prefix "[Rank]" or NameTag equipping system?

I want users to be able to equip different rank/nametags that they’ve unlocked, right now I only have [AlphaTester]. I have it to where if the player has the Alpha Tester badge then they unlock the option to equip the rank.
Screenshot 2024-05-13 005641
Screenshot 2024-05-13 005654
image

If the tag is unlocked then it will show a white checkmark rather than a locked icon (these are image buttons btw) and if equipped it will be a green check. My issue is that I have this functioning correctly, but I do not know how to make it to where if the user was to click the green checkmark again it would turn white and take off the [AlphaTester] rank/nametag from them in the chat.

Here is what I have so far:

local UserId = game.Players.LocalPlayer.UserId
local BadgeId = 1405951311851471
local Plr = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local player = game.Players.LocalPlayer
local box = player.PlayerGui.LobbyButtons.TitlesFrame.InsideBox.Box
local LockBn = box.ScrollingFrame.AlphaRankLabel.LockedIcon
----------

if BadgeService:UserHasBadgeAsync(UserId, BadgeId) then
	LockBn.Image = "rbxassetid://17479511743"
	LockBn.MouseButton1Click:Connect(function()
		LockBn.ImageColor3 = Color3.fromRGB(0, 185, 0)
		TextChatService.OnIncomingMessage = function(message: TextChatMessage)
			local properties = Instance.new("TextChatMessageProperties")
			if message.TextSource then
				local player = Plr:GetPlayerByUserId(message.TextSource.UserId)
				properties.PrefixText = "<font color='#9a3e3e'>[AlphaTester]</font> " .. message.PrefixText
			end
				return properties
		end
	end)
end

image

An example of the system I want, Death Ball and Creatures of Sonaria has this! Thanks.

You could check for the ImageColor3 of the button, and then assign only the default PrefixText to the prefix text property to remove the nametag.

LockBn.MouseButton1Click:Connect(function()
    if LockBn.ImageColor3 ~= Color3.fromRGB(0, 185, 0) then --Checkmark is white
        LockBn.ImageColor3 = Color3.fromRGB(0, 185, 0)
        TextChatService.OnIncomingMessage = function(message: TextChatMessage)
			local properties = Instance.new("TextChatMessageProperties")
			if message.TextSource then
				local player = Plr:GetPlayerByUserId(message.TextSource.UserId)
				properties.PrefixText = "<font color='#9a3e3e'>[AlphaTester]</font> " .. message.PrefixText
			end
			return properties
		end

    else --Checkmark is green
        LockBn.ImageColor3 = Color3.fromRGB(255, 255, 255)
        TextChatService.OnIncomingMessage = function(message: TextChatMessage)
			local properties = Instance.new("TextChatMessageProperties")
			if message.TextSource then
				local player = Plr:GetPlayerByUserId(message.TextSource.UserId)
				properties.PrefixText = message.PrefixText --Removed nametag
			end
			return properties
		end
    end
end)
1 Like

Tysm! Do you know how I can save it to if the player rejoins the tag is still there? I know it has something to do with player data but I’ve never messed with that stuff…

1 Like

Whenever the player clicks the checkmark, you could have a remote event that fires from client to server, telling the server to store the player nametag. It could be something like:

On the client side:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.RemoteEvent

if BadgeService:UserHasBadgeAsync(UserId, BadgeId) then
	LockBn.Image = "rbxassetid://17479511743"
	LockBn.MouseButton1Click:Connect(function()
    if LockBn.ImageColor3 ~= Color3.fromRGB(0, 185, 0) then

        --Tells server which nametag it is, and if the nametag was equipped
        RemoteEvent:FireServer(LockBn.Parent.Name, true) 

    else
        RemoteEvent:FireServer(LockBn.Parent.Name, false) 
    end
    end)
end

On a server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.RemoteEvent

local DataStoreService = game:GetService("DataStoreService")
local nametagStore = DataStoreService:GetDataStore("PlayerNametag") --create/access a datastore

RemoteEvent.OnServerEvent:Connect(function(player, nametag, equipped)

	local success, currentGold = pcall(function()
      if equipped then
        --Store the nametag, for the key name, I'd use the player's user id since it is unique
  		nametagStore:SetAsync(tostring(player.UserId, nametag))
      else
          nametagStore:SetAsync(tostring(player.UserId, ""))
      end
	end)

	if not success then
		print(errorMessage)
	end
end)

I would also recommend having a module script or a table to store the prefix text of the nametag for easier access; when a player joins the game, you can use datastore:GetAsync(keyname) to retrieve the player’s nametag, and get the prefix text by looking up the module script/table with the nametag, and then use the remote event to fire back to client to change the nametag.

For more information about data stores:
Data Stores | Documentation - Roblox Creator Hub

1 Like

Alright thank you for the help! I’ll read about data stores in the meantime.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.