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

How to you get a random player out of all of them?

Asked by 9 years ago

I'm not sure how to get all the players and randomly pick one to give a tool too.

I've tried for loops with getchildren.

Any ideas?

4 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Tables

Firstly, you should know something about indexing tables. Tables are setup like lists.

local tab = {'a',true,1}

1: 'a'

2: true

3: 1

Index 1 of tab is 'a'.

So, tab[1] is 'a'.


Applying math.random to indexing

Now, if we apply the math.random function to indexing the table then it will index it randomly!

local tab = {'a',true,1}

We can get the amount of indexes in tab with the # operator. So use #tab as the second argument in the math.random function.

local random = tab[math.random(1,#tab)]

Randomly Indexing The Players

The GetChildren function returns a table. So if we use the GetChildren function on game.Players then we can index it randomely.

local plrs = game.Players:GetChildren()

local random = plrs[math.random(#plrs)]

Awarding a Tool

Just clone a tool into their Backpack(:

local tool --Define the tool

tool:Clone().Parent = random.Backpack
Ad
Log in to vote
1
Answered by 9 years ago

Few things you'll need to know for this,:GetPlayers() is a method of the Players service that gets all the players and returns a table. math.random(startingValue, endingvalue) finds an Int between the two values. Startingvalue has to be smaller than endingvalue.

If we want to pick a random player you'll need:

1) Table of all the players

2) A random number

First we need to get the table of players using the :GetPlayers() method.

local players = game.Players:GetPlayers()

Now we need a random int between 1 and the amount of players in the table

local players = game.Players:GetPlayers()
local randomInt = math.random(1, #players)

That will find a whole number (int) between 1 and the number of values in the table

Now to get the player from the table using the random int we generated

local players = game.Players:GetPlayers()
local randomInt = math.random(1, #players)
local randomPlayer = players[randomInt]

Hope this helped!

0
Ah you beat me to it ;P iNicklas 215 — 9y
0
lol, I saw you join the chat as I was answering. I was wondering if you would answer it too YellowoTide 1992 — 9y
Log in to vote
0
Answered by
iNicklas 215 Moderation Voter
9 years ago

Use math.random

First off you need to get all players

local players = game.Players:GetPlayers()

Then define a player

random = math.random(1, #players)

And then just use

print(random.Name) 

Of course this doesn't answer your question fully, atleast try to show what you have made so we can help you :)

0
This will get you a random number, not a random player. DigitalVeer 1473 — 9y
Log in to vote
-4
Answered by 9 years ago

You could try it like you know how the admin command works like ':give random sword' Maybe you can try to do something like that. But don't use it as an admin command.

0
good idea. any other suggustions? ashpokeman5 2 — 9y
0
Use math.random iNicklas 215 — 9y

Answer this question