Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this team randomizer not working?

Asked by 8 years ago

This script is supposed to equally randomize teams. It doesn't seem to be working.

--shuffle players
team1=1
team2=0

for i,player in pairs(game.Players:GetPlayers()) do 
player.TeamColor=BrickColor.new("White")
end

players=game.Players:GetPlayers()

for i=1,#players do 
    repeat wait() n=math.random(1,#players) until players[n].TeamColor==BrickColor.new("White")
    if team1>team2 then
        team1=team1+2 
        players[n].TeamColor=mapclone.team1color.Value



    elseif team2>team1 then
        team2=team2+2 
        players[n].TeamColor=mapclone.team2color.Value



    end

    print(team1)
    print(team2)
end

All it seems to do is put players on the same team. I tried server multiplayer test, same team. I could use quick help, I'm on a deadline. Thanks!

0
Your for second loops does not have an end. You should also consider cleaning up your code, that is one reason why I think you couldn't debug it yourself. If you want, I could post a slightly different version of your script. TheDeadlyPanther 2460 — 8y
0
This is a snippet of the code. I'll fix it. ChemicalHex 979 — 8y
0
Give me a few minutes to figure this out. TheDeadlyPanther 2460 — 8y
0
I'm working on an answer GoldenPhysics 474 — 8y
0
Oh boy this is a lot ChemicalHex 979 — 8y

4 answers

Log in to vote
2
Answered by
Kryddan 261 Moderation Voter
8 years ago
--shuffle players
local team1 = 0
local team2 = 0

for i,player in pairs(game.Players:GetPlayers()) do 
    player.TeamColor=BrickColor.new("White")
end

local players = game.Players:GetPlayers()

for i = 1,#players do 
    if team2 > team1 or team1 == team2 then --I switched around with them a little
        team1=team1+1
        players[i].TeamColor= BrickColor.new("Really blue")
    elseif team1 > team2 then
        team2=team2+1
        players[i].TeamColor= BrickColor.new("Really red")
    end
    print(team1 .. "  team1")
    print(team2.. "  team2")
end

I tried this on studio test with 5 players and 3 people got placed in team 1 and 2 people in team 2.

0
Still no. ChemicalHex 979 — 8y
0
still the same problem? Kryddan 261 — 8y
0
Accepted for being the simplest and easiest to implement answer. ChemicalHex 979 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

I think i have the solution that you're looking for. To start though let me organize what we have. First everything that TheDeadlyPanther said wasn't wrong at all i will even use some of his code in this example. Secondly we know that we have a list of players and a set number of them where this list is returned in the same order provided players don't join or leave or an even number of players join or leave etc...

So how do we make this Random? We use the fisher-yates method. What this does is takes any list and randomizes all of its elements without creating or deleting any of them.

How it works :

lets say that we have a list of numbers 1 - 10 organized 1 - 10. start: 1 2 3 4 5 6 7 8 9 10

now we create a random argument from 1 - 10 for this example i will use 6. we then take the number at the 6th position out don't be confused here though we are NOT looking for the value 6 we want the value at index 6. we then place this in a new list as the first index. Next we take the last argument in the previous list in this case 10 and we make the randomly selected index 6 = the value at the last index 10.

old: 1 2 3 4 5 7 8 9 10 new: 6

we repeat this process until we have done this for all arguments in the first table.

now onto the code. So now that we know how to randomize a list we need to know how we can apply this to our situation of placing them on team 1 or 2. So im going to create 2 functions to handle this. the first one will return a player to give a team to and the 2rd one will decide what team to assign that player.

local team = 1 -- im going to use a flag variable instead of a counter

function giveTeam(player)
    if team == 1 then
        player.TeamColor = mapclone.team1color.Value
        team = 2
    else
        player.TeamColor = mapclone.team2color.Value
        team = 1
    end
end


function shufflePlayers()
    pList = {} -- table to hold the players

    for i, v in pairs(game.Players:GetPlayers()) do
        pList[i] = v
    end

    for i = 1, #pList do
        local index = math.floor(math.random(#pList - i)) + 1
        local value = pList[index]
        giveTeam(value)
        pList[index] = pList[#pList]
        table.remove(pList, #pList)
    end
end

hope that this helps

0
Line 4 you need double equal. Line 21 needs a do. GoldenPhysics 474 — 8y
0
sorry about my syntax gets annoying when you switch from languages all the time ProfessorSev 220 — 8y
Log in to vote
0
Answered by 8 years ago
--shuffle players
local team1 = 0
local team2 = 0

for i,player in pairs(game.Players:GetPlayers()) do 
    player.TeamColor=BrickColor.new("White") 
end

local players = game.Players:GetPlayers()
--local n = math.random(1,#players)

for i = 1,#players do 
    --repeat wait() n = math.random(1,#players) until players[n].TeamColor == BrickColor.new("White")
    if team1 > team2 or team1 == team2 then
        team1=team1+1
        players[n].TeamColor=mapclone.team1color.Value
    elseif team2 > team1 then
        team2=team2+1
        players[n].TeamColor=mapclone.team2color.Value
    end
    print(team1)
    print(team2)
end

Hopefully this worked. I changed a few things, but it should work.

Some tips:

  • Use tab to format your code, not space.
  • Put a lot of effort to keep things properly formatted, it will help a lot when editing later.
  • Try using in pairs for the second for loop.

Hope I helped :)

~TDP

0
Sadly, this does not work. It keeps one player neutral and the other on team1. ChemicalHex 979 — 8y
0
Hm. When is your deadline? I'll try and fix it when I get access to studio. I've also added some tips for you in the answer. TheDeadlyPanther 2460 — 8y
0
My deadline isn't harsh but I have a day or two. ChemicalHex 979 — 8y
0
Oh, I'll fix it later today then. It'll be in about 5-6 hours, if you can wait that long, because of school. TheDeadlyPanther 2460 — 8y
Log in to vote
0
Answered by 8 years ago
team1 = mapclone.team1color.Value
team2 = mapclone.team2color.Value

for i,player in pairs(game.Players:GetPlayers()) do 
    player.TeamColor=BrickColor.new('White')
end


for i, v in pairs(game.Players:GetPlayers()) do --for each player
    local currentPlayer = getTeam('White')[math.random(#getTeam('White'))]

    if #getTeam('White') <= (#getTeam(team1)-#getTeam(team2)) then --if there are barely enough players to make the teams even, make them even.
        currentPlayer.TeamColor = team2
    elseif #getTeam('White') <= (#getTeam(team2)-#getTeam(team1)) then --same as before
            currentPlayer.TeamColor = team1
    else
        local randomTeamNum = math.random() --get at random number
        if (randomTeamNum > (#getTeam(team1)/(#getTeam(team1)+#getTeam(team2)))) then --this bit does some fancy stuff
            --it makes it possible to be assigned either team
            --but makes it more likely to be assigned to the team with fewer players
            --based on the percentage of players between the two teams that are on the team.
            currentPlayer.TeamColor = team1
        else
            currentPlayer.TeamColor = team2
        end
    end
end

function getTeam(teamColor) --get the team or the number of players on the team
    local team = {} --make a table for the team players
    for i, v in pairs(game.Players:GetPlayers()) do --for each player
        if v.TeamColor == BrickColor.new(teamColor) then --if they're on team white
            table.insert(team, v); --put them in the table
        end
    end
    return team 
end

This does a bit of fancy chance stuff explained in the comments Have Fun!

I'll edit to add explanation or correct stuff if I need to.

Answer this question