Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get more random responses?

local players = game.Players:GetChildren()
local teams = {"Persimmon", "Cyan"}
local count = 1
for i, v in pairs(players) do
	v.TeamColor = BrickColor.new(teams[count])
        count = count + 1
	v.PlayerFolder:WaitForChild("Team").Value = tostring(v.TeamColor)
	if count > #teams then
		count = 1
	end
end

The whole point of this script is to get random teams for every player. The only problem I have noticed is that every round I'm on the exact same team. Can anyone please help?

3 comments

0

[disregard i misunderstood]

1

I mean that every single time I get the same team. When I have 3 people on I tried for 10 rounds, and all 3 of us got the exact same team

0

use math.randomseed to set a new seed, ideally the result of tick()

2 answers

✓ Accepted answer

Of course this isn't random. You're not using a single random function.

I think what you're trying to do is create separate teams with as equal number of players as possible. Because you're already doing this, I have an idea that will compliment with your method, without changing anything.

Just create a table with the players inside, but shuffled. This will create a table of players randomly indexed.

Below is the added code which does this, combined with your code.

local players = game.Players:GetPlayers() -- I changed this to GetPlayers instead of GetChildren
local teams = {"Persimmon", "Cyan"}
local count = 1

for i = #players, 2, -1 do
	local j = math.random (i)
	players[i], players[j] = players[j], players[i]
end

for i, v in pairs(players) do
	v.TeamColor = BrickColor.new(teams[count])
	count = count + 1
	v.PlayerFolder:WaitForChild("Team").Value = tostring(v.TeamColor)

	if count > #teams then
		count = 1
	end
end

I haven't tested this. If there is anything wrong, let me know.

More Info.

2 comments

0

You know, instead of saying iterations, you can just do for i = #players.... It's shorter than putting iterations anyway.

0

Yep, edited.

What you want is constrained random assignment:

  1. The team color of each player should be random

  2. At the same time, you have certain constraints, e.g. you want all teams to have about an equal amount of players

The script below satisfies both requirements. Note that LUA random implementation seems to not be "very random", but adding a bunch of extra calls to math.random() seems to help (see explanation below).

local teams = {"Persimmon", "Cyan"}

-- initialize team counts
local counts = {}
for i=1, #teams do counts[i]=0 end

-- determine how many players per team (at most)
local maxCount = math.ceil(#players / #teams)

-- re-randomize every time a game starts 
-- NOTE: because Lua random number implementation seems to be very weak, adding a bunch of extra calls to random() can help things a bit
-- reference: http://stackoverflow.com/questions/20154991/generating-uniform-random-numbers-in-lua
math.randomseed(tick())
math.random(); math.random(); math.random()

-- assign pseudo-random team to each player
for i, v in pairs(players) do
  -- random team index: keep rolling the dice until we get a "good" team
  local teamIndex
  repeat
    teamIndex = 1 + math.floor(math.random() * #teams)
    math.random()
    math.random()
    math.random()
    math.random()
  until counts[teamIndex] < maxCount

  -- update team player count
  counts[teamIndex] = counts[teamIndex]+1 
  
  -- get color from index
    local teamColor = teams[teamIndex]

  print(teamColor)

  -- use teamColor here    
end

This discussion is preserved as it was. New answers, comments, and votes are closed.