How to loop through all players in team(consistently)

i have problem i cannot loop through players in team. Assistants and other gpt make a ton of mistakes every time i want to change code, have you guys any suggestion how to do it?

i personally like making custom team system because its more flexible but if youre using roblox team system you can do something like this

→ loop through players
→ check their team

local PlayerService = game:GetService("Players")

function GetPlayersOnTeam(TeamNameToCheck)
local PlayersInTeam = {}
for i,v in pairs(PlayerService:GetPlayers()) do
if v.Team.Name ==  TeamNameToCheck then 
table.insert(PlayersInTeam,v)
end
return PlayersInTeam
end

GetPlayersOnTeam("Blue") --//  return all players that are in blue team

if you need help with specific code you would have to send a script

you have some problems its not consistantly and you forgot 1 end there is my code that dont work:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TEAM = game:GetService("Teams")
local playing = TEAM:GetTeams("Playing")

local function checkAllPlayers()
	for _, player in ipairs(Players:GetPlayers()) do
		local function cleanupTask()
			task:cancel()
		end
		
		if player.Team == game.Teams.Playing then
			if player.Character then
				local hum = player.Character:FindFirstChild("Humanoid")
				if hum:IsA("Humanoid") then
					while task.wait(5) do
						hum.JumpHeight += 5
					end
					if player.ParentRemoving and player.CharacterRemoving then
						cleanupTask()
						hum.JumpHeight = 7.2
					end
					
					
				end
			end
		end
	end
end



for i = 1, 10 do
	checkAllPlayers()
end

because its dont check every time i wanna change PlayerAdded to something what checking every time like while true

There is so much wrong with this code…

1 Like
local teams = game.Teams

local t1 = Instance.new("Team")
t1.Name = "Red"
t1.TeamColor = BrickColor.new("Really red")
t1.Parent = teams

local t2 = Instance.new("Team")
t2.Name = "Blue"
t2.TeamColor = BrickColor.new("Really blue")
t2.Parent = teams

local t1m = {}
local t2m = {}

local check = function()
	t1m = {}
	t2m = {}
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == t1 then
			table.insert(t1m,v)
		elseif v.Team == t2 then
			table.insert(t2m,v)
		end
	end
end

local play = coroutine.create(function()
	while task.wait(1) do
		-- blablabla
	end
end)

game.Players.PlayerAdded:Connect(function()
	check()
end()

coroutine.resume(play)

i think if you change things around you will get what you need. it might not work becuz i suck at coding. i hope this helps.

1 Like

Use RunService.Heartbeat, which runs at the end of every frame.

local function GetPlayersOnTeam(Team: string): { Player }
    --// ...your logic goes here
end

RunService.Heartbeat:Connect(function(_dt: number): ()
    local Players = GetPlayersOnTeam("YOUR_TEAM_NAME");
    --// ...
end)

BRO AWESOME Thank you(idk what coroutine mean right now i will learn that later)

FYI, the Team class has a “GetPlayers” method built into it:

local Teams = game:GetService("Teams")

print(Teams.Blue:GetPlayers())

nuh uh error saying GetPlayers not valid member of “Team”

Strange. Show me your code. In the meantime, you can visit the hyperlink in my previous reply to see that “GetPlayers” is indeed a component of the Team class

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.