|
| 13 Oct 2013 11:10 AM |
I want to select a random player on the game and do something to them. I do not know how to do this, although I know how to do many other things in scripting. Can someone give me the line of code for getting a random player? I'd assume it would start something like local players = game.Players:GetPlayers() |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 11:16 AM |
You got the starting part right!
local players = game.Players:GetPlayers() local randomPlayer = players[math.random(1, #players)] |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 11:20 AM |
| return game.Players:players()[math.random(1,#game.Players:players())] |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 11:22 AM |
Lua has a handy built-in function that picks a random number called math.random(). We can use this to retrieve a random player.
math.random takes two parameters(two things you give to the function), the starting number, and the ending number. So math.random(1,5) would give me a random number 1 through 5. We can also use this to retrieve a random player this way:
game.Players:GetChildren()[math.random(1,#game.Players:GetPlayers()]
When you put a number inside of square brackets after the :GetChildren() method, the script will refer to that number child of the selection. For instance, b = model:GetChildren()[5] would set b to the fifth object in your model. This works a similar way with players, but instead of putting a number in the square brackets, we used math.random(). Inside math.random() we put 1(the starting number), and the number of players as the second parameter. When you add a # in front of something you're getting the children of, the script will return the number of children in that object.
So in shorter terms, the script selects a random number(1 through the number of players in the game) and tells the next part of the script to use the number it chose. So if it chose 5, the game would use the fifth player. |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 11:24 AM |
| Lol Echo, I love how you take the time to explain everything. |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 11:26 AM |
| lol echo, I love how you copy and paste my script. |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 11:28 AM |
| Lol stephen, I love how you copy and paste my "Lol _____, I love how you..." |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 11:28 AM |
| @ stephen, your code is not unique. just cause someone wrote the same thing doesnt mean they copy/pasted |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
| |
|
|
| 13 Oct 2013 11:34 AM |
| Thank you all for your help, its been quite a turn out and extremely useful to me! |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 11:39 AM |
| No problem. We are all glad to help! |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 11:54 AM |
| Stephen, when you arrive at a viewing distance for my level of scripting, you can complain that I used the same line as you further proving that you're a novice scripter who thinks that he is an expert scripter for making a group entitled "Professional Scripters" |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 11:56 AM |
| @Echo, what is your level of scripting? |
|
|
| Report Abuse |
|
|
| |
|
|
| 13 Oct 2013 12:09 PM |
| @spy: Well into network security programming(Internship with Best Buy) |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 12:14 PM |
| And for those who know what they are, I have the CompTia A+ and Security+ certifications |
|
|
| Report Abuse |
|
|
Olama5
|
  |
| Joined: 31 Jan 2010 |
| Total Posts: 2827 |
|
| |
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 01:02 PM |
| Nice job Echo. I'm taking programming in college right now. Introduction to C#! :D |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 01:03 PM |
| Oh, btw. I always laugh when someone tells me ROBLOX is for little kids. XD |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2013 01:11 PM |
| Same here. Though I'm not in college yet(I will be next year), my high school has a IT Networking program which allowed me to get my A+ last year. I self-taught myself for the Network+ just in case I might need it for a job. |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 01:24 PM |
| That's so awesome how you self taught yourself. I never heard of Network+ though. Anyway, I'm only a junior right now. My state has this program called "Running Start" which allows you to go to college and get high school and college credits at the same time (You must be a junior or senior though). So by the time I graduate from college, I will have an Associates Degree and a High School Diploma. :D |
|
|
| Report Abuse |
|
|
spynaz
|
  |
| Joined: 17 Feb 2011 |
| Total Posts: 1326 |
|
|
| 13 Oct 2013 01:25 PM |
| And what's even more awesome is then you can go straight to a university (which I will do) right after you finish the 2 years of college. |
|
|
| Report Abuse |
|
|