Function That Detects Player's Team

Hello everyone, I just have some trouble determining how to make a function so that it detects the player’s team and prints it on the console. I might be doing something wrong, so I am open to any suggestions. I am aware that I am not allowed to ask for people to write code for me, so if anyone would point me in the right direction, that would be great.
My code:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local function detectTeam(player)
	if player.Team == Teams.TeamA then
		print("You are TeamA")
	end
end

detectTeam()

Output:

  09:16:39.566 - Players.bootsareme.PlayerGui.MenuControl:34: attempt to index nil with 'Team'

Note: I have two teams named TeamA and TeamB already. My script is in StarterGui because I am trying to prompt GUI’s to the respective teams.

1 Like

You’ve made your script confused try this

local Players = game:GetService("Players")
local TeamService = game:GetService("Teams")

local function detectTeam(player)
	if player.Team == TeamService.TeamA then
		print("You are TeamA")
	end
end

detectTeam()

You haven’t defined what player you want to target. Do this:

local player = game.Players.LocalPlayer

if player.Team == "TeamA" then
   print("You are TeamA")
end

I don’t think that’ll work, since the Team property is an object, not a string.

1 Like

The problem with your code here is that Teams is undefined, because you called your variable Team. Rename the variable to Teams and it should work.

1 Like

You usually detect a team via its color eg:

Player.TeamColor == "Bright Blue"

Hopefully that helps if it works.

-uD0ge

I find this to be pretty unreliable, if you change the color of a team you’ll have to go through all of your code and change it.

I found the solution, thanks to @FlashFlame_Roblox and @brokenVectors for giving me insight.

local Teams = game:GetService("Teams")
local player = game.Players.LocalPlayer

local function detectTeam()
    if player.Team == Teams.TeamA then
	    print("You are TeamA")
    end
end

detectTeam()