The Script below just picks 1 random player out of a server, a lot of it is irrelevant to my question
01 | local gameon = false |
02 |
03 |
04 | while gameon = = false do |
05 | gameon = true |
06 | intermission = 15 |
07 | wait(intermission) |
08 | local players = game.Players:GetChildren() |
09 | if #players > 0 then |
10 | local pickedplayer = math.random( 1 , #players) |
11 | print (pickedplayer) |
12 | gameon = false |
13 | else |
14 | gameon = false |
15 | wait( 5 ) |
16 | end |
17 | end |
Well I think you know a bit about tables, but in case if you don't, here
So what you are doing is almost right, but the pickedplayer
variable isn't actually a player, it's number that is randomly generated from 1 to #player
(aka how many players there are).
This number is the index of the player, so technically if you wanna get the player you wanna do
players[pickedplayer]
, since players is a table, we have to put a [] next to it, and give the index of the chosen players inside of it, which should give us what we want which is the player.
And then you just do players[pickedplayer].Name
which is the name property of the player.
We can also rearange it a bit to make it better.
01 | local gameon = false |
02 |
03 |
04 | while gameon = = false do |
05 | gameon = true |
06 | intermission = 15 |
07 | wait(intermission) |
08 | local players = game.Players:GetChildren() |
09 | if #players > 0 then |
10 | local player = players [ math.random( 1 , #players) ] |
11 | print (player.Name) |
12 | else |
13 | gameon = false |
14 | wait( 5 ) |
15 | end |
16 | end |
01 | local gameon = false |
02 |
03 |
04 | while gameon = = false do |
05 | gameon = true |
06 | intermission = 15 |
07 | wait(intermission) |
08 |
09 |
10 | local players = game.Players:GetPlayers() |
11 | if #players > 0 then |
12 | local pickedplayer = math.random( 1 , #players) |
13 | local ChosenPlayer |
14 | for i,v in paris(players) do |
15 | if i = = pickedplayer then |
The script will loop through all the players in the server until the random number matches i in the loop. Then it will take the player out of the loop so you can use it for other things.
1 | local pickedplayer = --You know |
2 | local player = game.Players:FindFirstChild(pickedplayer) |
The script finds the first player that has pickedplayer
in his name, so if someone's name is the pickedplayer, s/he will be the chosen one
1 | local gameon = false |
2 | wait( 15 ) |
3 | local gameon = true |
4 |
5 |
6 | local players = game.Players:GetChildren() |
7 | local total = #players |
8 | local playerchoosen = players [ math.random( 1 ,total) ] |
9 | print ( "Player choosen is " ..playerchosen.Name) |
I haven't tested it to see if it works, but I'm pretty sure it will anyway.