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
    print("Start round")
        Randomizer = 1
        for i,v in pairs(Players) do
            print(Randomizer/ 3)
            print(math.floor(Randomizer))
            if Randomizer / 3 == math.floor(Randomizer) then
                v.TeamColor = BrickColor.new("Navy blue")
            else
                v.TeamColor = BrickColor.new("Bright red")
            end
            Randomizer = math.floor(Randomizer) + 1
        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

Randomizer/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

function randomizeTable(tbl) -- A very useful function from StickMasterLuke
    math.randomseed(tick())
    local returntbl={}
    if tbl[1]~=nil then
        for i=1,#tbl do
            table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
        end
    end
    return returntbl
end

local CopPrisonerRatio = 3 -- how many prisoners for each cop
local TotalPlayers = #(game:GetService("Players"):GetChildren())

local copsPicked = 0

local numberOfCops = math.floor((TotalPlayers/(CopPrisonerRatio+1)) + 0.5)--gets how many cops will be chosen

local playerList = randomizeTable(game:GetService("Players"):GetPlayers())

    for i,v in pairs(playerList ) do
    if v then
     if  copsPicked<numberOfCops then
               v.TeamColor = BrickColor.new("Navy blue")
          else
             v.TeamColor = BrickColor.new("Bright red")
         end
    end  
    end

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