this is a question
because both players are sometimes in the same team but i dont want to if they are 2 players
so how to prevent that
i want to achieve this:
so 1 player on the team and 1 player on the other team
this is a question
because both players are sometimes in the same team but i dont want to if they are 2 players
so how to prevent that
i want to achieve this:
so 1 player on the team and 1 player on the other team
without using the math.random thingy
You could do something like this:
if (team1 - team2) > 1 then
-- a random player moves to team 2.
elseif (team2 - team1) > 1 then
-- a random player moves to team 1.
end
Is there any particular reason why you don’t want to use math.random?
anybody? i need this in order to fix my game
It’s hard to understand the issue from your posts because theres just to many problems and solutions going on. It would help if you posted your current code, what’s wrong with it, and what you already tried to fix it.
i want to get both players in variables so i dont have to use math.random
the code is a long line btw
local players = game:GetService("Players"):GetPlayers()
local player1 = players[1]
local player2 = players[2]
is all I can extrapolate from your question without seeing that long line.
hmmm yeah that worked for me thx
Honestly you won’t get any real help unless you give more details on your issue. I still don’t really see why math.random wouldn’t work. I think you should reword your initial post.
because it can pick the same number and i dont want that to happen so how can i prevent it then
Yes, it can pick the same number, but I don’t really see why that’s such an issue. If there are too many players in a team why does it matter which person is moved?
Alright, so your problem is this: “I want to split all players into two-equally sized, random teams. My problem with using math.random()
is that a random function does not ensure that both teams are equal size.”
Very simple solution, instead of pulling player names from a queue and then rolling a dice to decide which team they go to, you first choose a team non-randomly and then pull player names from a hat.
local players = game:GetService("Players"):GetPlayers()
local team1 = {}
local team2 = {}
for i = 1, math.floor(#players/2) do
local r = math.random(1, #players)
table.insert(team1, players[r])
table.remove(players, r)
end
for i = 1, math.ceil(#players/2) do
local r = math.random(1, #players)
table.insert(team2, players[r])
table.remove(players, r)
end
I haven’t tested this but I imagine it will work.
Using a bool value to see which team does a player go into next, you set a variable local Team = true
and we will do it like this, if Team == true then you are going to red team, if not then you are going to blue team, now we loop through the players by using
local Players = game.Players:GetPlayers()
for _, Player in pairs(Players) do
if Team == true then
Player:SetTeam("RED")
else
Player:SetTeam("BLUE")
end
end
Now all we need to do is to change the team color the next player is getting assigned to by doing Team = not Team
, this inverts the current value so if it was true then it will be false and vice versa, here is how our script should look in the end:
local Team = true
local Players = game.Players:GetPlayers()
for _, Player in pairs(Players) do
if Team == true then
Player:SetTeam("RED") -- YOU CHANGE THIS TO THE SCRIPT YOU USE FOR SWITCHING TEAMS
else
Player:SetTeam("BLUE") -- DO SAME THING HERE
end
Team = not Team
end
I hope this helps
local teams = game:GetService("Teams"):GetTeams() -- teams you want to fill players into
for i, player in pairs(game.Players:GetPlayers()) do
player.TeamColor = teams[1 + (i % #teams)].TeamColor
end
I was actually going to suggest tabulating the teams and then if the teams are unbalanced do a math.random on the index of the team with too many people, and that random player would be moved. Although the OP has been given enough to go on already, so it doesn’t really matter.