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: Iterate through set number of random players?

Previous Thread :: Next Thread 
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 10:53 AM
In my code, I wish to make it so that if you run
randPlayer(function(4, plyr)
print(plyr.Name);
end)

it would log four different random player's names, none of which the same

This is what I have so far, I know I would need a for loop of some sort above the callback, just no idea what to do from here. Thanks

function getPlayers(callback)
for i, v in pairs(game.Players:GetPlayers()) do callback(v) end
end
function randPlayer(numPlyrs, callback)
local players = game.Players:GetPlayers()
if (callback and numPlyrs) then
callback(players[math.random(#players)]);
elseif (not numPlyrs) then
return players[math.random(#players)];
elseif (callback) then
callback(players[math.random(#players)]);
end
end


-iJava/199MS
Report Abuse
NetsFan0 is not online. NetsFan0
Joined: 17 Apr 2009
Total Posts: 2777
18 Oct 2014 11:02 AM
Put the 4 players, in a table, return that and iterate through it.
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 11:11 AM
@Nets
I don't know how I would find 4 different random players though
so that none of them are the same

I know how to do that part though


-iJava/199MS
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 11:29 AM
bump


-iJava/199MS
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Oct 2014 11:40 AM
Is this what you mean?

function randPlayers(toPick, players)
if toPick > #players then
return "Not enough players"
end

local picked = {}
while #picked < toPick do
table.insert(picked, table.remove(players, math.random(#players))) -- table.remove returns the removed value, so we can put this in one line
end

return picked
end

local picked = randPlayers(4, game.Players:GetPlayers())
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 11:48 AM
So that would return 4 random players from the game, yes?


-iJava/199MS
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Oct 2014 11:50 AM
Yes, it would. You can also change the first argument to determine how many players you wish to.
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 11:53 AM
Thanks!


-iJava/199MS
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 11:54 AM
How can I put that in a callback so I can just do this:
randPlayer(function(4, plyr)
print(plyr.Name);
end)

and it would print their names?

Thanks again btw


-iJava/199MS
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Oct 2014 11:57 AM
for i, player in pairs(picked) do
print(player.name)
end
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 12:02 PM
Thanks, got it


-iJava/199MS
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 12:07 PM
This isn't working

function randPlayers(toPick, callback)
local players = game.Players:GetPlayers();
if toPick > #players then
print("Not enough players");
else
local picked = {}
while #picked < toPick do
table.insert(picked, table.remove(players, math.random(#players))) -- table.remove returns the removed value, so we can put this in one line
end
for i,player in pairs(picked) do
callback(player)
end
end
end


function startGame()
local picked = randPlayers(4, function(plyr)
print(plyr.Name);
end)
end
startGame();



-iJava/199MS
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Oct 2014 12:10 PM
Are you testing it with 4 or more players in the game?
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 12:16 PM
I changed it to 1 player instead of 4 for the testing, nothing was printed
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 12:23 PM
Any idea why it doesn't work?


-iJava/199MS
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
18 Oct 2014 12:25 PM
Nevermind, I fixed it

Thanks for the help though, I appreciate it :)


-iJava/199MS
Report Abuse
Doccc is not online. Doccc
Joined: 15 Sep 2013
Total Posts: 3370
18 Oct 2014 12:26 PM
Give me your fedora and all shall be resolved
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