Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I get the chosen players name?

Asked by 5 years ago

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


4 answers

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

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

0
wayyyy better than my take on it. Fad99 286 — 5y
0
nice try though! starmaq 1290 — 5y
Ad
Log in to vote
1
Answered by
Fad99 286 Moderation Voter
5 years ago
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.

0
really nice answer! but there is a better way to do it starmaq 1290 — 5y
Log in to vote
0
Answered by
WoTrox 345 Moderation Voter
5 years ago
Edited 5 years ago
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

0
pickedplayer is actually a number, it isn't the player object starmaq 1290 — 5y
Log in to vote
0
Answered by 5 years ago
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.

Answer this question