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
|
  |
| 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
|
  |
| 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
|
  |
| Joined: 12 Aug 2014 |
| Total Posts: 41286 |
|
| |
|
|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 12 Aug 2014 |
| Total Posts: 41286 |
|
| |
|
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 |
|
|
|
| 18 Oct 2014 11:57 AM |
for i, player in pairs(picked) do print(player.name) end |
|
|
| Report Abuse |
|
|
Widths
|
  |
| Joined: 12 Aug 2014 |
| Total Posts: 41286 |
|
|
| 18 Oct 2014 12:02 PM |
Thanks, got it
-iJava/199MS |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 18 Oct 2014 12:10 PM |
| Are you testing it with 4 or more players in the game? |
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| Joined: 15 Sep 2013 |
| Total Posts: 3370 |
|
|
| 18 Oct 2014 12:26 PM |
| Give me your fedora and all shall be resolved |
|
|
| Report Abuse |
|
|