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

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

Asked by 4 years ago
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!

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

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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
Ad

Answer this question