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 )
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!