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?
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?
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'.
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)]
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)]
Just clone a tool into their Backpack(:
local tool --Define the tool
tool:Clone().Parent = random.Backpack
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:
Table of all the players
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!
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 :)
This will get you a random number, not a random player.
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.
good idea. any other suggustions?
Use math.random
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
Ah you beat me to it ;P
lol, I saw you join the chat as I was answering. I was wondering if you would answer it too