01 | while wait() do |
02 | for i = 5 , 0 ,- 1 do |
03 | --Lobby Timer |
04 | wait(sec) |
05 | print (i.. " / Before" ) |
06 | end |
07 | for _, player in pairs (game.Players:GetPlayers()) do |
08 | --Player Monster |
09 | local randomplayer = player [ math.random( 1 , #player) ] |
10 | end |
11 | end |
The output say that player is a userdata value and i d'ont know how to fix this
well, the reason might be that player, is well... a player object and not a table so you might want to try something on the lines of this
1 | math.randomseed(tick()) |
2 | while true do |
3 | local plrs = game.Players:GetPlayers() |
4 | local rIndex = math.random( 1 ,#plrs) |
5 | local randomplr = plrs [ rIndex ] |
6 | wait() |
7 | end |
So things to take away:
1 | 1 ) don't use while wait() do end , use while true do wait() end |
2 | 2 ) use math.randomseed(tick()) for "better randomness" |
If you're trying to select a random player, that's not the way to do it.
Try this:
1 | while wait() do |
2 | for i = 5 , 0 ,- 1 do |
3 | --Lobby Timer |
4 | wait(sec) |
5 | print (i.. " / Before" ) |
6 | end |
7 | local players = game.Players:GetPlayers() |
8 | local randomplayer = players [ math.random( 1 , #players) ] |
9 | end |
a for loop
literates through a table, and the variable is the object, not a table which you'd use a # operator to get a length. this means you dont need to use a for loop
to get a random player, but just use game.Players:GetPlayers()
, math.random
and the #
operator alone.
01 | while true do |
02 | for i = 5 , 0 ,- 1 do |
03 | --Lobby Timer |
04 | wait(sec) |
05 | print (i.. " / Before" ) |
06 | end |
07 | local players = game.Players:GetPlayers() |
08 | local chosenPlayer = players [ math.random( 1 ,#players) ] |
09 |
10 | print (chosenPlayer.Name) -- example |
11 | -- do stuff here |
12 |
13 | -- you dont need a wait as theres a for loop which yields |
14 | end |