Need help applying teams

Hello devform,

Recently I’ve been making nametags for my game, and I need help making the nametags change-able from the player’s GUI.

The teams are stored in a modulescript, here’s the structure for it:

local teams = {}

teams.Something = {

Name = "Something",
Color = Color3.fromHex("HexCodeHere"),

Subroles = {
["SubRole1"] = {
Name = "HelloWorld",
Color = Color3.fromHex("HexCodeHere"),
Staff = false,
Pass = 0
},

}

return teams

This is relatively simple, for each one, Pass is the gamepass that the user will be prompted to buy if they do not have it already to access the team
Staff is if it is restricted to users who are marked as staff, and will only be accessible to them.

This module is designed for multiple teams and all of their subroles.
From an eartlier post, I have figured out how to install nametags, that is not the problem.

The current issue is trying to figure out how to apply changes to the nametags.
I’ve tried this through multiple different ways, which have either been complicated or confuse me too much to figure out.

Here is the localscript that sends the information of the changes:

-- Services
local RepStore = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local plr = Players.LocalPlayer

-- RemoteEvent reference (Optional for validation purposes)
local ClickEvent = RepStore.Nametags:WaitForChild("TagInfo")

-- Function to handle button clicks and switch teams
local function onButtonClicked(role, subrole)
	-- Optionally use RemoteEvent for validation
	ClickEvent:FireServer(role, subrole)
end

-- Function to setup button connections
local function setupButtons(parentFrame, role)
	for _, button in ipairs(parentFrame:GetChildren()) do
		if button:IsA("TextButton") then
			local subrole = button.Name
			button.MouseButton1Click:Connect(function()
				onButtonClicked(role, subrole)
			end)
		end
	end
end

-- Setting up buttons for different roles
local Subroles = script.Parent.Subroles
setupButtons(Subroles.Staff.Name, "Staff")

Here’s the regular script that receives the changes:

local RepStore = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

-- Function to handle team switching
local function switchTeam(player, role, subrole)
	if role == "Staff" then
		if subrole == "Creator" then
			player.Team = Teams.Creator
		elseif subrole == "Contributor" then
			player.Team = Teams.Contributor
		end
	end
end



-- RemoteEvent to handle team switching (Optional for validation purposes)
local buttonClickedEvent = RepStore.Nametags:WaitForChild("TagInfo")
buttonClickedEvent.OnServerEvent:Connect(function(player, role, subrole)
	switchTeam(player, role, subrole)
end)

Yes I have tried chatgpt, and I ask you to ignore the parts where it establishes the Teams service.
Any help is appreciated.

For your switchTeam method, you don’t have to hardcode them unless the subrole is different from the team’s name. You can just use Teams:FindFirstChild(subrole).

As for the issue you’re having, I do see anywhere that you interface/reference a nametag. Obviously, this should be done on the server-side, probably inside your switchTeam method. Assuming the tag is a BillboardGui inside the player’s character, you have access to the player object within the switch teams method so you can just obtain their tag and change its text.

Hi, and sorry for this incredibly late reply

I’ve updated the server script according to your reply and this is the new one:

local RepStore = game:GetService("ReplicatedStorage")
local Teams = require(script.Parent.Teams)

-- Function to handle team switching
local function switchTeam(player, role, subrole)
	if role == "Staff" then
		if subrole == "Creator" then
			player.Team = Teams.Creator
		elseif subrole == "Contributor" then
			player.Team = Teams.Contributor
		end
	end
end



-- RemoteEvent to handle team switching (Optional for validation purposes)
local buttonClickedEvent = RepStore.Nametags:WaitForChild("TagInfo")
buttonClickedEvent.OnServerEvent:Connect(function(player, role, subrole)
	switchTeam(player, role, subrole)
end)

I’ve asked the Roblox AI Assistant, and sadly it claims that the :FindFirstChild() function can’t be used on a ModuleScript. (and to be honest, that’s dumb.)

I mean, I wouldn’t mind sitting for hours specifying and writing if statements, but if there’s a much faster and more efficient way of doing this, then I’d prefer that.