|
| 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
|
  |
| Joined: 19 Sep 2012 |
| Total Posts: 13336 |
|
|
| 27 Jul 2016 01:53 PM |
v.TeamColor = teams[math.random(1, #teams) ??
|
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 27 Jul 2016 02:11 PM |
| ^^Your teams are unbalanced. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 02:13 PM |
@Durst
What if I want to leave out the Neutral team?
|
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 02:13 PM |
and yes, they are very unbalanced.
|
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 27 Jul 2016 02:18 PM |
@Cas Mine arent being sorted with your code :/
|
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|