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

bad argument #2 to 'random' (interval is empty) error?

function CloneMeteor()
				local Meteor = game.ServerStorage.Fireball
				local Clone = Meteor:Clone()
				local xPos = math.random(21, -21) -- Error comes on this line
				local yPos = 20
				local zPos = math.random(99, -99)
					
				Clone.Parent = workspace
				Clone.Position = Vector3.new(xPos, yPos, zPos)
end

Whenever I run this code in a test server I get this error: bad argument #2 to 'random' (interval is empty)

I'm not sure what this means, I'm running this as a ServerScript in ServerScriptService. If anyone could help me that'd be great, thanks!

1 comment

0

-21 is lower than 21, not higher. To fix this, do this. math.random(-21,21) as it goes from lowest to highest.

1 answer

✓ Accepted answer

math.random starts with the small number and end with the bigger number, not big to small like you have it. This is causing your script to error:

local xPos = math.random(21, -21)

This is also causing your script to error:

local zPos = math.random(99, -99)

You should change those two lines of code to be this instead:

local xPos = math.random(-21, 21) 
local zPos = math.random(-99, 99)

Full script:

function CloneMeteor()
                local Meteor = game.ServerStorage.Fireball
                local Clone = Meteor:Clone()
                local xPos = math.random(-21, 21) -- Error comes on this line
                local yPos = 20
                local zPos = math.random(-99, 99)

                Clone.Parent = workspace
                Clone.Position = Vector3.new(xPos, yPos, zPos)
end

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