generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Getting a random player...

Previous Thread :: Next Thread 
BlockzieCartI is not online. BlockzieCartI
Joined: 28 Sep 2009
Total Posts: 812
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 is online. 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
stephen1182 is not online. stephen1182
Joined: 31 Aug 2010
Total Posts: 4003
13 Oct 2013 11:20 AM
return game.Players:players()[math.random(1,#game.Players:players())]
Report Abuse
EchoReaper is online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
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 is online. 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
stephen1182 is not online. stephen1182
Joined: 31 Aug 2010
Total Posts: 4003
13 Oct 2013 11:26 AM
lol echo, I love how you copy and paste my script.
Report Abuse
spynaz is online. 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
PRESTIGIOUSaLEGEND is not online. PRESTIGIOUSaLEGEND
Joined: 16 Apr 2011
Total Posts: 1765
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 is online. spynaz
Joined: 17 Feb 2011
Total Posts: 1326
13 Oct 2013 11:28 AM
XD
Report Abuse
BlockzieCartI is not online. BlockzieCartI
Joined: 28 Sep 2009
Total Posts: 812
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 is online. spynaz
Joined: 17 Feb 2011
Total Posts: 1326
13 Oct 2013 11:39 AM
No problem. We are all glad to help!
Report Abuse
EchoReaper is online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
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 is online. spynaz
Joined: 17 Feb 2011
Total Posts: 1326
13 Oct 2013 11:56 AM
@Echo, what is your level of scripting?
Report Abuse
PRESTIGIOUSaLEGEND is not online. PRESTIGIOUSaLEGEND
Joined: 16 Apr 2011
Total Posts: 1765
13 Oct 2013 11:57 AM
well said Echo
Report Abuse
EchoReaper is online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
13 Oct 2013 12:09 PM
@spy: Well into network security programming(Internship with Best Buy)
Report Abuse
EchoReaper is online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
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 is not online. Olama5
Joined: 31 Jan 2010
Total Posts: 2827
13 Oct 2013 12:19 PM
@Echo
Your epic!
Report Abuse
spynaz is online. 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 is online. 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
EchoReaper is online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
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 is online. 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 is online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image