|
| 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
|
  |
| Joined: 23 Apr 2011 |
| Total Posts: 1795 |
|
| |
|
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 |
|
|
|
| 08 May 2016 06:48 PM |
Both suggestions seem to not work
|
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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 |
|
|
| |
|
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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 08 May 2016 08:02 PM |
It works! Thanks for that. =D
|
|
|
| Report Abuse |
|
|
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 |
|
|