Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 9 years ago

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.

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. IXLKIDDO 110 — 9y
0
Its Just spaces/ Blank Lines ioutragous 0 — 9y

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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.
0
Thank you it helped alot, ioutragous 0 — 9y
Ad

Answer this question