Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you detect if one player is on a team?

I know this is not a request site but I honestly have no clue. I have been trying to do this for the past 4 hours. I can't find anything on the internet that helped.

This is what I have so far:

Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
    -- This is where I'm stuck
end

6 answers

Is this what you want?

local Team = Wherever the team is located
local player = Team:GetPlayers()
if #player == 1 then
	print("1 player found in Team")
	--run rest of code here
end

4 comments

0

The code looks correct. I will try this.

0

It doesn't work.

1

what do you mean, and what do you want it to do?

0

please show how you used this code.

To detect if one player is on a certain team all you need is the player's name. A player has a property that determines the team it is on, so you can check that way.

If PlayerName.Team == game.Teams.TeamName then
	--do stuff
end

As far as I'm sure, this method is for server side only.

3 comments

0

This would not work because you need to determine PlayerName.

0

What are you trying to do exactly?

0

I want to make it so if there is ONE player left on a team the game will end. But the only line of code I need is to detect if there is one player left. Sorry if I'm not clear.

There is a Player.Team property inside the player.

1 comment

0

Yeah but how do you detect if there is ONE player on a certain team.

so you would do:

local PlayerOnTeam = 0
for _,i in pairs(game.Players:GetChildren()) do
	if i.Team == game.Team.Teamname then
		PlayerOnTeam = PlayerOnTeam + 1
	end
end
if PlayerOnTeam = 1 then
	do thing if only one player
end

6 comments

0

i just made a change

0

Alright.

0

Fixed a few typos. I'm about to test it.

0

This does not work.

0

is there an error i quickly typed it up the the chance of an error is high

0

if there is not an error maby add a print(PlayerOnTeam) and put it on line 7 and push line 7 out of the way

game.Players.PlayerAdded:Connect(function(p)
if p.Team == ("TeamName) then
-- Code
end
end)

1 comment

1

you forgot a closing quotation mark

An easy solution would be to fire a remoteFunction or remoteEvent depending on what you want the result to be, but here is a way to find the player's team

function onPlayerAdded(player)
if player.Team == ("YourTeamName") then
--Your code.
end
end)

game.Players.PlayerAdded:Connect(onPlayerAdded)

If this doesn't work I'm sorry but i was typing this from memory and haven't actually tested it out on studio.

This discussion is preserved as it was. New answers, comments, and votes are closed.