My Code to find the a Random Player Keeps Erroring with this code: ServerScriptService.AutoJoin:15: bad argument #2 (interval is empty)
I don’t even know why it is erroring out
Here is the Relevant code:
local Num = 0
local PlayerArray = {}
local runGame = true
game.Players.PlayerAdded:Connect(function(player)
Num = Num + 1
table.insert(PlayerArray, player)
print("PlayerAdded")
end)
game.Players.PlayerRemoving:Connect(function(player)
Num = Num - 1
end)
function startGame()
runGame = false
if Num <= 2 then
local random = math.random(1,Num) --This is What is Erroring out
print(PlayerArray[random])
end
end
while runGame==true do
startGame()
end
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().
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