Only picking 1 not 2

I want it to pick from the teams folder it is not picking 2 its only picking 1 team

local teams = game.ServerStorage.Teams:GetChildren()
local team = teams[(math.random(2, #teams))]:Clone()
team.Parent = game.Teams
3 Likes

Yes because math.random(2,#teams)) means like if there are 2 teams and the minimum from the math random is also 2, it will only pick one.

Try using

local team = teams[(math.random(1, #teams))]:Clone()

instead?

3 Likes

There’s more than 2 teams​​​​

3 Likes

Still can’t figure out​​​​​​​​

1 Like

So if I understand correctly, you want to pick two teams, including the first option in the list, which your current code does not allow. This is because the bounds of math.random (as @ThobbJack mentioned above) are set to (2, #teams) . Let’s say #teams = 30. Your code will only pick one team listed from 2 to 30 and never pick the first team. Instead, you should write:

local team = teams[(math.random(1, #teams))]:Clone()

However, even this still selects only one team, although it now includes the first team as an option for random selection. Now you need to create two different math.random statements, with local team1selection and local team2selection. Then, on top of that, (ignore this part if it is okay to select two of the same team type for the teams), you will need to ensure that team1 and team2 do not select the same team type. We will use while instead of if because there is a tiny chance it may select the same two teams twice. Here is the full code below:

local teams = game.ServerStorage.Teams:GetChildren()
local team1selection = math.random(1, #teams)
local team2selection = math.random(1, #teams)

while team1selection == team2selection do
    team2selection = math.random(1, #teams)
end

local team1 = teams[team1selection]:Clone()
team1.Parent = game.Teams

local team2 = teams[team2selection]:Clone()
team2.Parent = game.Teams
3 Likes

You need to set the random seed of math.random between calling it:

math.randomseed(os.time())
print(math.random(2, 10)

What will this do exactly​​​​​​​

I see, thanks for clarifying​​​​​​

1 Like

I’ll put yours as solution for everyone else

1 Like

math.random will return the same number each time it’s called unless you set its randomseed (the seed by which numbers are generated) to something constantly changing, like the current time: os.time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.