Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

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
0
The code looks correct. I will try this. CheckeredDevAlt 25 — 3y
0
It doesn't work. CheckeredDevAlt 25 — 3y
1
what do you mean, and what do you want it to do? BrainDead_Dev 135 — 3y
0
please show how you used this code. BrainDead_Dev 135 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

0
This would not work because you need to determine PlayerName. CheckeredDevAlt 25 — 3y
0
What are you trying to do exactly? ImNotKevPlayz 4 — 3y
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. CheckeredDevAlt 25 — 3y
Log in to vote
0
Answered by 3 years ago

There is a Player.Team property inside the player.

0
Yeah but how do you detect if there is ONE player on a certain team. CheckeredDevAlt 25 — 3y
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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
0
i just made a change botw_legend 502 — 3y
0
Alright. CheckeredDevAlt 25 — 3y
0
Fixed a few typos. I'm about to test it. CheckeredDevAlt 25 — 3y
0
This does not work. CheckeredDevAlt 25 — 3y
View all comments (2 more)
0
is there an error i quickly typed it up the the chance of an error is high botw_legend 502 — 3y
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 botw_legend 502 — 3y
Log in to vote
0
Answered by 3 years ago
game.Players.PlayerAdded:Connect(function(p)
if p.Team == ("TeamName) then
-- Code
end
end)
1
you forgot a closing quotation mark BrainDead_Dev 135 — 3y
Log in to vote
0
Answered by 3 years ago

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.

Answer this question