The Script below just picks 1 random player out of a server, a lot of it is irrelevant to my question
local gameon = false while gameon == false do gameon = true intermission = 15 wait(intermission) local players = game.Players:GetChildren() if #players > 0 then local pickedplayer = math.random(1, #players) print(pickedplayer) gameon = false else gameon = false wait(5) end 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.
local gameon = false while gameon == false do gameon = true intermission = 15 wait(intermission) local players = game.Players:GetChildren() if #players > 0 then local player = players[math.random(1, #players)] print(player.Name) else gameon = false wait(5) end end
local gameon = false while gameon == false do gameon = true intermission = 15 wait(intermission) local players = game.Players:GetPlayers() if #players > 0 then local pickedplayer = math.random(1, #players) local ChosenPlayer for i,v in paris(players) do if i == pickedplayer then ChosenPlayer = v -- v is the selected player end end print(ChosenPlayer) -- this will print the player that has been selected gameon = false else gameon = false wait(5) end end
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.
local pickedplayer = --You know 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
local gameon = false wait(15) local gameon = true local players = game.Players:GetChildren() local total = #players local playerchoosen = players[math.random(1,total)] print("Player choosen is "..playerchosen.Name)
I haven't tested it to see if it works, but I'm pretty sure it will anyway.