Help on Random Script

Hello, I really need some help on a script

  1. I really want to make a Player Randomizer

  2. My Code to find the a Random Player Keeps Erroring with this code: ServerScriptService.AutoJoin:15: bad argument #2 (interval is empty)

  3. I don’t even know why it is erroring out

Here is the Relevant code:

local Num = 0
local PlayerArray = {}
local runGame = true
	Num = Num + 1
	table.insert(PlayerArray, player)
	Num = Num - 1
function startGame()
	runGame = false
	if Num <= 2 then
	    local random = math.random(1,Num) --This is What is Erroring out
while runGame==true do

This exception occurs when the first number is greater than the second. Num is likely zero.

It’s highly unnecessary to make your own table and update it when a player joins when you can simply call GetPlayers() on the players service to get a table of all players.

So instead of using your Num variable, you can simply do this:

local playersTable = game.Players:GetPlayers()

local randomPlayer = math.random(1, #playersTable)

And as @incapaxx said, Num is probably 0, so you must use GetPlayers().

1 Like

You are checking if Num is less than 2 here, so this will only fire when that occurs. You also don’t need the PlayerArray, Roblox does this for you with the function:

PlayersService:GetPlayers() -- // returns array of all current players