I'm trying to make a round system that'll randomly pick a player for a role, it does work when there is only one player, but fails when there are multiple players.
Snippet:
1 | local RandomPlayer = game.Players:GetChildren() [ math.random( 1 ,#game.Players:GetPlayers()) ] |
2 | if RandomPlayer.Team.Name = = "Lobby" then |
3 | RandomPlayer.TeamColor = BrickColor.new( "Lapis" ) |
4 | RandomPlayer.Character:WaitForChild( "Humanoid" ).JumpPower = 0 |
5 | workspace.OfficeChair.Seat:Sit(RandomPlayer.Character:WaitForChild( "Humanoid" )) |
6 | game.ReplicatedStorage.NightGuardLocal:FireClient(RandomPlayer) |
7 | end |
Output:
1 | 21 : 59 : 41.895 - ServerScriptService.Script: 18 : bad argument # 2 to 'random' (interval is empty) |
2 | 21 : 59 : 41.907 - Stack Begin |
3 | 21 : 59 : 41.908 - Script 'ServerScriptService.Script' , Line 18 |
4 | 21 : 59 : 41.908 - Stack End |
if your trying to make a script that picks a random player I wouldn't do it using remove functions? I would do it in the main game loop if you're doing a mini game or mini game mechanics. What you would want to do is something more like this :
1 | local PlayerTable = { } |
2 |
3 | for i,player in pairs (game.Players:GetPlayers()) do |
4 | table.insert(PlayerTable,player.Name) |
5 | end |
6 |
7 | local RandomPlayer = game.Players:FindFirstChild(PlayerTable [ math.random( 1 ,#PlayerTable) ] ) |
What this dose is it will get all the players in the game and put them in a table. Then it will get one random player using > math.random(1,#PlayerTable)]