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 » Scripters
Home Search
 

Re: How do I make my script choose players randomly

Previous Thread :: Next Thread 
applejacks12 is online. applejacks12
Joined: 29 Mar 2010
Total Posts: 30
08 May 2016 06:09 PM
Hey I'm having trouble with a script I'm making. I want it to choose players randomly but it won't work. Here's the script:

local players = game.Players:GetChildren()
local player = game.Players.LocalPlayer

while true do
local chosenplayer = players[math.random(1, #game.Players)]
end


Report Abuse
Crimsonal is online. Crimsonal
Joined: 23 Apr 2011
Total Posts: 1795
08 May 2016 06:11 PM
game.Players:GetPlayers()
Report Abuse
gum444 is not online. gum444
Joined: 06 May 2011
Total Posts: 549
08 May 2016 06:32 PM
local ply = game.Players:GetPlayers()[math.random(game.Players.NumPlayers)]
wait(3)
m.Text = ply.Name.." has beem chosen!"
Report Abuse
applejacks12 is online. applejacks12
Joined: 29 Mar 2010
Total Posts: 30
08 May 2016 06:48 PM
Both suggestions seem to not work


Report Abuse
Lord_Narwhal is not online. Lord_Narwhal
Joined: 05 May 2012
Total Posts: 3242
08 May 2016 06:51 PM
local pTable = {}
for i,v in pairs(game.Players:GetPlayers()) do
if v then
table.insert(pTable,v.Name)
local randomPlayer = pTable[math.random(1,#pTable)]
print(randomPlayer)
end
end


Report Abuse
Dev_Ryan is online. Dev_Ryan
Joined: 10 Mar 2013
Total Posts: 243
08 May 2016 07:00 PM
First off, if you use a loop, you need to include a wait() or else your game will crash.

Like so:

while true do
wait()
-- code here
end

But you don't need to do that. You only need to make it a function and call it whenever you want it to get a random player.

You can do that like so:

function getRandomPlayer()
local players = game.Players:GetPlayers()
local randomNumber = math.random(1,#players)
return players[randomNumber]
end

local chosenPlayer = getRandomPlayer()

print(chosenPlayer.Name.." was chosen!")


Report Abuse
applejacks12 is online. applejacks12
Joined: 29 Mar 2010
Total Posts: 30
08 May 2016 07:22 PM
none of them are working


Report Abuse
Dev_Ryan is online. Dev_Ryan
Joined: 10 Mar 2013
Total Posts: 243
08 May 2016 07:42 PM
They do work, you are just doing something wrong in your script.

Perhaps you are using a loop at the top of the script making the code at the bottom not run at all? Or maybe you are using the function incorrectly. Check to see if there is any errors, using Script Analysis, or printing to the console output to see if the code is being run at all. Sometimes I make my scripts disabled and forget to enable them so check for that too. If you need help fast, you can post the entire script here and we will take a look at it for you.


Report Abuse
applejacks12 is online. applejacks12
Joined: 29 Mar 2010
Total Posts: 30
08 May 2016 07:56 PM
This is what the output says when I put in the script.

20:55:47.591 - ServerScriptService.Script:3: bad argument #2 to 'random' (interval is empty)
20:55:47.592 - Script 'ServerScriptService.Script', Line 3 - global getRandomPlayer
20:55:47.592 - Script 'ServerScriptService.Script', Line 7
20:55:47.593 - Stack End


Report Abuse
Lord_Narwhal is not online. Lord_Narwhal
Joined: 05 May 2012
Total Posts: 3242
08 May 2016 07:58 PM
OH lol we're all idiots(in a joking manner)

it's saying the interval is empty because the players haven't loaded quite yet

try putting

repeat wait() until game.Players:GetPlayers()


Report Abuse
applejacks12 is online. applejacks12
Joined: 29 Mar 2010
Total Posts: 30
08 May 2016 08:02 PM
It works! Thanks for that. =D


Report Abuse
Dev_Ryan is online. Dev_Ryan
Joined: 10 Mar 2013
Total Posts: 243
08 May 2016 09:25 PM
Ah yea, I didn't think about that. Usually when I use a script like this, there is already at least 1 player in the game.

So that means I don't have to do something like:

repeat wait() until game.Players.NumPlayers > 0
or
repeat wait() until #game.Players:GetPlayers() > 0


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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