The long title says it. I need help making the game decide the amount of Cops will be assigned depending on the amount of players in the server.
So when a round begins e.g Round 1,2,3, & so on and so forth. The game will be randomizing the assigned teams. I want the game to assign a random amount of players to Cops & Thieves teams according to the amount of players in the server right now.
So if there were only two players like 1v1, The script will decide 1 cop, 1 thief. If there are 5 players, 2 cops will be assigned, 3 thieves will be assigned.
So yeah, any ideas on how can I do that? Reply below.
task.wait(20) -- for test only
local PlayerCount = #game.Players:GetChildren() -- Gets the number of children it has
local DivideTeams = PlayerCount / 2 -- Change 2 to 3 if theres 3 teams
local tointeger = math.floor(DivideTeams + 0.5)
local ThievesAreLoaded = false
local CopsAreLoaded = false
print(tointeger)
for c, selecteduser in pairs(game.Players:GetChildren()) do
-- Thieves
if c ~= tointeger and not ThievesAreLoaded then
selecteduser.TeamColor = game.Teams.Thief.TeamColor
elseif c == tointeger then
ThievesAreLoaded = true
end
-- Cops
if DivideTeams == math.floor(DivideTeams) then -- if its a decimal
print("Divided teams are decimal")
if c ~= tointeger - 1 and not CopsAreLoaded then
selecteduser.TeamColor = game.Teams.Cop.TeamColor
elseif c == tointeger then
CopsAreLoaded = true
end
elseif DivideTeams == math.floor(DivideTeams + 0.5) then -- its a integer
print("Divided teams are integer")
if c ~= tointeger and not CopsAreLoaded then
selecteduser.TeamColor = game.Teams.Cop.TeamColor
elseif c == tointeger then
CopsAreLoaded = true
end
end
end
But my problem is, it worked on 2 players(even numbers of players) but i was trying to test it on odd(3) number players and what happened is it crashed. My internet is very slow so it took me long to got it back from autosave. Also, this won’t crash on 3 players mode if the players computer is not in the same computer as the player.
I don’t see why you have to wrestle with that pairs loop like this.
workspace.ReadyButton.Touched:Wait() -- step on a part with one of your testing players to activate this script
local players = game.Players:GetChildren()
local amount = #players
local ratio = 0.5 -- half
-- shuffle the table
-- without this, the list of players might be {Player1, Player2, Player3, Player4} etc.
-- and Player1 and Player2 will always be on the same team...
-- I did not write this code
for i = #players, 2, -1 do
local j = math.random(i)
players[i], players[j] = players[j], players[i]
end
-- assign first half of players
-- if there are 3 players, 1 out of 3 will be Cops (for i = 1, 1.5)
for i = 1, amount * ratio do
local player = players[i]
player.TeamColor = game.Teams.Cop.TeamColor
end
-- assign second half of players
-- this will get up to one more player: for i = 2, 3
for i = math.floor(amount * ratio + 1), amount do
local player = players[i]
player.TeamColor = game.Teams.Thief.TeamColor
end
edit: I could’ve written this line more cleverly:
for i = math.floor(amount * ratio + 1), amount do
-- to:
for i = amount, amount * ratio, -1 do
… but good code is understandable, not clever.
(It counts from the end to the middle instead of counting from the middle to the end)