Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why do i get a Workspace.Script:5: bad argument #2 to 'random' (interval is empty) error?

Why does this code give me an error? CODE:


local Players = game.Players:GetPlayers()
local Phantom = Players[math.random(1, #Players)]

Error: Workspace.Script:5: bad argument #2 to 'random' (interval is empty) Please Help, Thanks for reading.

2 comments

0

Can you post the whole code? If the error is on line 5 and we can't see everything, it's a bit harder to find out what's wrong.

0

Its Just spaces/ Blank Lines

1 answer

✓ Accepted answer

Most likely this is because there are no players in the game. math.random expects the first number to be lower than the second, and throws an error if it isn't true.

You're going to have to check if game.Players.NumPlayers is > 0 before running that code:

while game.Players.NumPlayers < 1 do
	wait()
end

local Players = game.Players:GetPlayers()
local Phantom = Players[math.random(#Players)] --The `1, ` is implied here.

1 comment

0

Thank you it helped alot,

This discussion is preserved as it was. New answers, comments, and votes are closed.