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

Random team selector don't work!? (please help)

Asked by
tomekcz 174
5 years ago
Edited 5 years ago
01print("Start round")
02    Randomizer = 1
03    for i,v in pairs(Players) do
04        print(Randomizer/ 3)
05        print(math.floor(Randomizer))
06        if Randomizer / 3 == math.floor(Randomizer) then
07            v.TeamColor = BrickColor.new("Navy blue")
08        else
09            v.TeamColor = BrickColor.new("Bright red")
10        end
11        Randomizer = math.floor(Randomizer) + 1
12    end

so it should work like that 3 rd person should be in team "Nave blue" but its not happening

0
the teams would be unfair Fifkee 2017 — 5y
0
its prisoners and cops game... tomekcz 174 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

So The problem is

1Randomizer/3 == math.floor(Randomizer)

will never equal each other here is why

if Randomizer = 1 then Randomizer/3 = 0.333 math.floor(Randomizer) = 1

if Randomizer = 2 then Randomizer/3 = 0.6666 math.floor(Randomizer)=2

if Randomizer = 3 then Randomizer/3 = 1 math.floor(Randomizer)=3

if Randomizer = 4 then Randomizer/3 =1.33333 math.floor(Randomizer)=4

and so on

in the end you will never be able to get a blue person on the cops team or it will at least take a massive amount of players for only one blue the other problem would be that technically the teams would never be random as it goes in order so it would pick the same people for the same roles.

So the alternative that is random and will still make the same 1:3 ratio is something like this

01function randomizeTable(tbl) -- A very useful function from StickMasterLuke
02    math.randomseed(tick())
03    local returntbl={}
04    if tbl[1]~=nil then
05        for i=1,#tbl do
06            table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
07        end
08    end
09    return returntbl
10end
11 
12local CopPrisonerRatio = 3 -- how many prisoners for each cop
13local TotalPlayers = #(game:GetService("Players"):GetChildren())
14 
15local copsPicked = 0
View all 29 lines...

Its much longer than what you made but it will make sure the teams are randomized and that both teams will random players.

If this does not work for you I am sorry and I will remove this post to prevent it from misleading others.

0
Somebody already helped me at chat, but thanks for answer tomekcz 174 — 5y
Ad

Answer this question