generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How can I make this random?

Previous Thread :: Next Thread 
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 01:48 PM
Hey guys. I have this script which places players into teams. However, every round, each player is sorted into the exact same team. How can I randomise this? Thx!

local players = game.Players
local teams = game.Teams:GetTeams()

for i,v in ipairs(players:GetPlayers()) do
v.TeamColor = teams[i%#teams+1].TeamColor
end


Report Abuse
RobuxLife is not online. RobuxLife
Joined: 19 Sep 2012
Total Posts: 13336
27 Jul 2016 01:53 PM
v.TeamColor = teams[math.random(1, #teams) ??


Report Abuse
RobuxLife is not online. RobuxLife
Joined: 19 Sep 2012
Total Posts: 13336
27 Jul 2016 01:53 PM
Try this maybe v.TeamColor = teams[math.random(1, #teams)]


Report Abuse
RobuxLife is not online. RobuxLife
Joined: 19 Sep 2012
Total Posts: 13336
27 Jul 2016 01:55 PM
This works, I've tested it. local players = game.Players
local teams = game.Teams:GetTeams()

for i,v in ipairs(players:GetPlayers()) do
v.TeamColor = teams[i%#teams+1].TeamColor
end



Report Abuse
RobuxLife is not online. RobuxLife
Joined: 19 Sep 2012
Total Posts: 13336
27 Jul 2016 01:56 PM
Oops, sorry for the mistake..

local players = game.Players
local teams = game.Teams:GetTeams()

for i,v in ipairs(players:GetPlayers()) do
v.TeamColor = teams[math.random(1, #teams)]
end


Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Jul 2016 02:02 PM
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
function shuffleTeams()
local players = Players:GetPlayers()
local teams = Teams:GetTeams()
for i = 1, #players do
table.remove(players,math.random(#players)).TeamColor = teams[i%#teams+1].TeamColor
end
end
Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:04 PM
@Casu
20:04:26.952 - ServerScriptService.Game:31: attempt to index global 'players' (a nil value)
20:04:26.952 - Stack Begin
20:04:26.952 - Script 'ServerScriptService.Game', Line 31
20:04:26.953 - Stack End


Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Jul 2016 02:10 PM
My code works fine, as is my code cannot generate that error because in my code 'players' is a local, not a global.

Post your new code
Report Abuse
DurstAuric is online. DurstAuric
Joined: 12 May 2009
Total Posts: 8066
27 Jul 2016 02:10 PM
local Players, Teams = game:GetService("Players"), game.Teams:GetTeams() math.randomseed(tick() * math.random())
for Pos, Player in pairs(Players:GetChildren()) do
Player.TeamColor = Teams[math.random(Pos % #Teams + 1)].TeamColor
end


( ͡• ◡ ͡•) -=[ RAP: 348,889 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•)
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Jul 2016 02:11 PM
^^Your teams are unbalanced.
Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:13 PM
@Durst

What if I want to leave out the Neutral team?


Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:13 PM
and yes, they are very unbalanced.


Report Abuse
DurstAuric is online. DurstAuric
Joined: 12 May 2009
Total Posts: 8066
27 Jul 2016 02:15 PM
Did you not follow what the other guy said before?

local Players, Teams = game:GetService("Players"), {"Team name", "Another team name"} math.randomseed(tick() * math.random())
for Pos, Player in pairs(Players:GetChildren()) do
Player.TeamColor = Teams[math.random(Pos % #Teams + 1)].TeamColor
end


( ͡• ◡ ͡•) -=[ RAP: 348,879 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•)
Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:18 PM
@Cas
Mine arent being sorted with your code :/


Report Abuse
DurstAuric is online. DurstAuric
Joined: 12 May 2009
Total Posts: 8066
27 Jul 2016 02:20 PM
Just read casual's script, that would work better than mine. Here.

local PlayerService, Teams = game:GetService("Players"), {"Team name", "Another team"} math.randomseed(tick() * math.random())
RandomTeams = function()
local Players = PlayerService:GetChildren()
for Player = 1, #Players do
table.remove(Players, math.random(#Players)).TeamColor = Teams[Player % #Teams + 1].TeamColor
end
end


( ͡• ◡ ͡•) -=[ RAP: 348,880 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•)
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Jul 2016 02:23 PM
My code does work. I tested it before I posted. I'm not sure how you are modifying my code, but you've apparently modified it to produce the error you posted above (as my code cannot produce that error as presented).

Post what you have now so we can see what it is you're doing.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
27 Jul 2016 02:25 PM
#When you are too ignorant to understand how to use someones code.


Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:26 PM
This is what I have:

local PlayerService, Teams = game:GetService("Players"), {"Keepers", "Escapers"} math.randomseed(tick() * math.random())
RandomTeams = function()
local Players = PlayerService:GetChildren()
for Player = 1, #Players do
table.remove(Players, math.random(#Players)).TeamColor = Teams[Player % #Teams + 1].TeamColor
end
end

Literally, the players are staying in the Neutral Team. No errors or nothing.


Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:32 PM
Forgot it was a function.

Called it, and got this error on this line:
table.remove(Players, math.random(#Players)).TeamColor = Teams[Player % #Teams + 1].TeamColor


Error - 20:31:50.225 - ServerScriptService.Game:25: bad argument #3 to 'TeamColor' (BrickColor expected, got nil)


Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Jul 2016 02:34 PM
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
math.randomseed(tick()%1*1e7)
function RandomTeams()
local players = Players:GetPlayers()
local teams = {Teams["Keepers"],Teams["Escapers"]}
for i = 1, #teams do table.insert(teams, 1, table.remove(teams,math.random(#teams))) end
for i = 1, #players do
local player = table.remove(players, math.random(#players))
player.TeamColor = teams[i % #teams + 1].TeamColor
player.Neutral = false
end
end
RandomTeams()
Report Abuse
AlvinBLOXX is online. AlvinBLOXX
Joined: 22 Feb 2011
Total Posts: 1557
27 Jul 2016 02:36 PM
It worked! <3

Thanks so much to everyone who replied to my forum with support - It's people like you who we need more of in our community. :-)


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image