How can you check if a player is on a certain team?

  1. What do I want to achieve? Basically the script is supposed to work so if you walk onto the basketball court while a game is going on and your not on either team, you get teleported outside of the court.

  2. What is the issue? The script is not working with checking if whether or not the player is on a team. It teleports you outside of the court regardless of whether or not you’re on a team.

  3. What solutions have you tried so far?

Here are the two methods I’ve tried so far for checking if a player is on a team when they hit the basketball court.

local OnAteam = false
if game.Teams.Red:FindFirstChild(hit.Parent.Name) then
	print("it worked")-- checking if player is on red team (not working)
	OnAteam = true -- if player is on a team, this comes back true
end
print(OnAteam) -- comes back false again

Second Method:

local OnAteam = false

for i, v in pairs (game.Teams:WaitForChild("Red"):GetChildren()) do
    print(v.Name)
	if v.Name == hit.Parent.Name then
		OnAteam = true
	end
end

print(OnAteam) -- comes back false

I was on the red team when I went into court and each time it came back false.

5 Likes

You can do

plr.Team == "Red"

Team will be the Team’s name, 90% sure. If not you can use plr.TeamColor and check for that

1 Like

ROBLOX has some trouble checking team names against Player.Team. I have worked a lot with teams in my games and in the past I have found success comparing team colors rather than team names. This has the unfortunate side effect of not being able to use two teams with the same TeamColor

local Teams = game:GetService("Teams")

local TeamOne = Teams:WaitForChild("Red")
local TeamTwo = Teams:WaitForChild("Blue")

local function playerIsOnTeam(player)
	return (player.TeamColor == TeamOne.TeamColor) or (player.TeamColor == TeamTwo.TeamColor)
end

print(playerIsOnTeam(player))

To avoid such side effects, you can determine if the player’s team is the same as the one in game.Teams by doing something like this

local Teams = game:GetService("Teams")

local TeamOne = Teams:WaitForChild("Red")
local TeamTwo = Teams:WaitForChild("Blue")

local function playerIsOnTeam(player)
	return (player.Team == TeamOne) or (player.Team == TeamTwo)
end

print(playerIsOnTeam(player))
2 Likes

You can use

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

game.Players.PlayerAdded:Connect(function(player)

if player.Team == Teams["(team name)"] then -- add, or Teams["(team name)"] to add more teams to it

end

end)

I hope this works for you :grinning:

10 Likes

I wish I could give two solutions out because both of the solutions from @bonejon and @Zyrun work! Thank you so much for your help.

3 Likes

No problem i already had that in my scripts because i needed that to.

Im glad it worked😀

3 Likes

nice i think this will help me in making complex sytems

1 Like