I am looking to script a system that when prompted, teams each player to a different team in the game. I found this script online, it made sense so I tried it out and it worked. I then attempted to incorporate it with another script, first it would team the players once a certain player count had been reached and then it would refresh them.
while true do
wait(15)
if PlayerCount >= 4 and MatchProgress == false then
for _, player in pairs(game.Players:GetChildren()) do
player.TeamColor = BrickColor.new(teams[number])
number = number + 1
if number > #teams then
number = 1
end
end
for i,v in pairs (game.Players:GetChildren()) do
print("Game beginning")
if v.Character then
v.Character.Humanoid.Health = 0
end
end
MatchProgress = true
end
end
However it only refreshes the player’s and does not team them?
Variables;
local teams = {"Lapis","Medium stone grey","Pearl", "Ghost grey", "Bright bluish green", "Medium red", "Really red", "Sand red", "Light blue", "Artichoke", "Carnation pink", "Camo"}
local number = 1
There are more variables that set the player count;
Players.PlayerAdded:Connect(function(player)
PlayerCount = PlayerCount + 1
print("Player count is currently at "..PlayerCount)
end)
Players.PlayerRemoving:Connect(function(player)
PlayerCount = PlayerCount - 1
print("Player count is currently at "..PlayerCount)
end)
local Players = game:GetService("Players")
local MatchProgress = false
local RequiredPlr = 4
local PlayerCount = 0
Through a little rearranging and polish of your current code, you can achieve your desired effect. You’re probably experiencing an error of some kind, which you haven’t indicated in the post. Make sure to use your console well, it’s there for a reason.
local Players = game:GetService("Players")
-- Alternatively; dynamically collect team colours over hard-coding them
local Teams = {"Lapis", "Medium stone grey", "Pearl", "Ghost grey", "Bright bluish green", "Medium red", "Really red", "Sand red", "Light blue", "Artichoke", "Carnation pink", "Camo"}
--[[ Dynamic collection example:
local Teams = game:GetService("Teams")
local TeamColours = {} -- Rename table to prevent conflicting variable names
for _, Team in pairs(Teams:GetTeams()) do
table.insert(TeamColours, Team.TeamColor.Name)
end
]]
local MatchProgress = false
local RequiredPlayerCount = 4
local PlayerCount = 0
local TeamIndice = 1
-- ToDo: Account for players already in-game, as PlayerAdded may not catch them
Players.PlayerAdded:Connect(function ()
-- Do not hold a reference to the player if not needed in the function scope
PlayerCount = PlayerCount + 1
end)
Players.PlayerRemoving:Connect(function ()
PlayerCount = PlayerCount - 1
end)
-- Use pseudothreading methods (spawn/coroutine) or place while loops at end
-- Code after a while loop does not run until the loop terminates
while true do
-- Don't think there's any reason to sleep at the beginning of an iteration
if PlayerCount >= 4 and not (MatchProgress) then
-- Keeping this at the top makes things more readable
MatchProgress = true
-- GetPlayers is canonical to fetch players
-- Reuse your service variable
for _, player in pairs (Players:GetPlayers()) do
player.TeamColor = BrickColor.new(Teams[TeamIndice])
if player.Character and player.Character:FindFirstChild("Humanoid") then
player.Character.Humanoid.Health = 0
end
-- Fake ternary to shorten count; if not readable to you, don't use
TeamIndice = (TeamIndice > #Teams and 1 or TeamIndice + 1)
end
end
end