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

: invalid argument #2 to 'random' (interval is empty)?

Im just trying to do some randomization for a projectile, but for whatever reason this little bit of code is outputting the error ": invalid argument #2 to 'random' (interval is empty)" and I dont know why.

local XRandom = math.random((0 - InaccuracyPossibility.X ), InaccuracyPossibility.X )

2 comments

0

It worked fine for me.

0

Post all of your code here so I know what to do.

1 answer

✓ Accepted answer

Looks like your running in to your X value going negative math.random() doesn't like negative values. So I would Flip the X Value to positive then pass the new value to the math.random() function then times the value by-1 if it was negative

For example in code: (Old answer Code)[INT]

local InaccuracyPossibility = Vector3.new(-5,1,2)

for i=1, 10 do
	local bisXNeg = InaccuracyPossibility.X <= 0 -- See if the XValue is negative or not

	local XAbs = math.abs(InaccuracyPossibility.X) -- Grab the abs of x

	-- pass the Abs(X) then if its negative then times this value by -1

	local XRandom = math.random((0 - XAbs), XAbs ) * (bisXNeg == true and -1 or 1)
	print(XRandom)
end

Output:

 2 
 -3
 -1
 -0
2
-1
-0
4
-1
3

(New Answer Code)[NUMBER(FLOAT)]

local InaccuracyPossibility = Vector3.new(-5.02,1.2,2.5)

for i=1, 10 do
    local bisXNeg = InaccuracyPossibility.X <= 0 -- See if the XValue is negative or not

    local XAbs = math.abs(InaccuracyPossibility.X) -- Grab the abs of x

    -- pass the Abs(X) then if its negative then times this value by -1
	-- Removed the (0-XAbs) and replaced it with the following
	local XRandom = (math.random()*XAbs) * (bisXNeg == true and -1 or 1)
    print(XRandom)
end

Output:

  -2.5070609002733
  -0.61484114847888
  -1.7726778006567
  -1.0249097790085
  -0.40516958552847
  -2.1358409614483
  -3.7283219967974
  -4.8174073688361 
  -1.6732552332573
  -4.0884841332942

i havent tested it in Roblox but it's working in notepad++ with a lua plugin! (im on my phone lolz) :P

hope this helps!

7 comments

0

humm, thats a little bit of a problem, becuase im trying to choose between a range of numbers, from negative to positive and I need to use the Vecetor3 of InaccuracyPossibility to do so.

0

try it, it does negative and positive! the magic happens when you times a value by -1 eg 5 * -1 = -5 or -5 * -1 = 5

0

Problem is, Im not dealing with whole number integers, so for whatever reason, math.random appears to simplifying the numbers im using.

0

hmm ill see what i can come up with. is the bit (0 - XAbs) needed in your math.random() @ line10?

0

Well, what ive tried to do is just use math.random(0, math.abs(InnacuracyPossibility)), that way I get that number, then run that through a short little 'coin-flip' script to determine if I need to negate it or not. Im working on that rn, il see where I get.

0

just added to my answer to include non- whole numbers since calling math.random() without any values returns a value between 0 and 1, we just times that by your value then flip it if your value was negative to start with!.

0

Alright, i managed to get this working in a way, I had to create a function then do the 'coin-flip' in the function then set the the respective values to thoes functions (im probaby over complicating this) but it does semi-work. I say semi because it does everything it should, except for the fact that math.random appears to be simplifies the number I input. im just gonna ask another question.

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