I am making a team randomizer so half the players are on a team and the other half is on the other.
But the problem is that either all players are on the same team or half the players stay neutral.
How do I fix the randomizer?
Code
for i = 1,NumPlrs do
local Players = game.Players:GetPlayers()
if i >= NumPlrs/2 or i >= math.ceil((NumPlrs/2) + .5) then
print("Not dig")
local Num = math.random(1,#Players)
Players[Num].Team = game.Teams.Team1 --Not atcual name
table.remove(Players,Num)
else
print("Dig")
local Num = math.random(1,#Players)
Players[Num].Team = game.Teams.Team2 --Not atcual name
table.remove(Players,Num)
end
end
The readability of you’re script was difficult for me, so I remade the script if you’d like to use the one below.
To reply to what @TerryMichaelBrunk said, you can set the Player.Team property to a Team object. The most likely reason @Dalbertjdplayz’s script was not working was probably due to the math, which in all honestly I understood no bit of.
local Players = game.Players:GetPlayers()
local DigUsers = 0
local NotDigUsers = 0
for i, v in ipairs(Players) do
if DigUsers > NotDigUsers then
NotDigUsers = NotDigUsers + 1
v.TeamColor = game.Teams.Team1.BrickColor
elseif NotDigUsers > DigUsers then
DigUsers = DigUsers + 1
v.TeamColor = game.Teams.Team2.BrickColor
elseif NotDigUsers == DigUsers then
NotDigUsers = NotDigUsers + 1
v.TeamColor = game.Teams.Team1.BrickColor
end
end
local count = script.count--IntValue
local Players = game:GetService("Players")
local AllPlayersCount = #Players:GetPlayers()
local player = Players.LocalPlayer
local Teams = game:GetService("Teams")
local team1 = Teams.team1
local team2 = Teams.team2
local m = math.random(1,5)--5 is max number of players in server
local playerteam = player.Team
count.Value = AllPlayersCount
if count.Value==1 then return playerteam==team1
end
if AllPlayersCount>1 then if m==1
then do playerteam=team2 if m==2 then
end
-- or you can do something like
local teamscount = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
local teamplayers = team:GetPlayer()
if teamplayers>3 then
if m==1 then do player.Team=team1
if m==2 then player.Team=team2
--etc...
end
end
The problem might be because the Player variable is reset every iteration. Maybe do something like:
local Players = game.Players:GetPlayers()
for i = 1,NumPlrs do
if i >= NumPlrs/2 or i >= math.ceil((NumPlrs/2) + .5) then
print("Not dig")
local Num = math.random(1,#Players)
Players[Num].Team = game.Teams.Team1 --Not atcual name
table.remove(Players,Num)
else
print("Dig")
local Num = math.random(1,#Players)
Players[Num].Team = game.Teams.Team2 --Not atcual name
table.remove(Players,Num)
end
end