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

01local gameon = false
02 
03 
04while gameon == false do
05gameon = true
06intermission = 15
07wait(intermission)
08local players = game.Players:GetChildren()
09if #players > 0 then
10local pickedplayer = math.random(1, #players)
11print(pickedplayer)
12gameon = false
13else
14    gameon = false
15    wait(5)
16    end
17end

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.

01local gameon = false
02 
03 
04while gameon == false do
05gameon = true
06intermission = 15
07wait(intermission)
08local players = game.Players:GetChildren()
09if #players > 0 then
10local player = players[math.random(1, #players)]
11print(player.Name)
12else
13    gameon = false
14    wait(5)
15    end
16end
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
01local gameon = false
02 
03 
04while gameon == false do
05gameon = true
06intermission = 15
07wait(intermission)
08 
09 
10local players = game.Players:GetPlayers()
11if #players > 0 then
12local pickedplayer = math.random(1, #players)
13local ChosenPlayer
14for i,v in paris(players) do
15    if i == pickedplayer then
View all 25 lines...

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
1local pickedplayer = --You know
2local 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
1local gameon = false
2wait(15)
3local gameon = true
4 
5 
6local players = game.Players:GetChildren()
7local total = #players
8local playerchoosen = players[math.random(1,total)]
9print("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