Invalid argument #2 to 'random' (interval is empty)

Idk how this error appeared.It was working find just a few moments ago. I only rotated the whole map but nothing else.

here is my code:

local rp = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

local point1 = script.Parent.Point1
local point2 = script.Parent.Point2

local folder = Instance.new("Folder", workspace)
folder.Name = "StarterBalls"

local models = rp.Models:WaitForChild("Enemies")

while task.wait(.1) do
	
	local amount = #folder:GetChildren()
	
	if amount < 200 then
		local chance = math.random(1,100)
		local model
		if chance == 1 then
			model = models:WaitForChild("YellowSphere")
		elseif chance ~= 1 and chance <= 20 then
			model = models:WaitForChild("CyanSphere")
			
		elseif chance > 20 and chance <= 50 then
			model = models:WaitForChild("SpikedSphere")
		else
			model = models:WaitForChild("RedSphere")
		end
		
		local clone = model:Clone()
		
		if clone.Name == "SpikedSphere" then
			debris:AddItem(clone, 60)
		end
		
		clone.Parent = folder
		local x = math.random(math.round(point1.Position.X), math.round(point2.Position.X))
		local y = math.random(math.round(point1.Position.Y), math.round(point2.Position.Y))
		local z = math.random(math.round(point1.Position.Z), math.round(point2.Position.Z))
		
		clone.Sphere.Position = Vector3.new(x,y,z)
		
	end
end
1 Like

this can happen when the max argument of math.random is less than the minimum, are you sure point2’s positions are bigger than point1?

2 Likes

@karbis is correct, the first argument has to be a lower number than the second.

Since the points are constant, instead of ensuring that coordinates of Point2 are greater than Point1 coordinates, it’s easier to establish intervals by comparing values for individual axes.

For example:

local intervals = {
	x = {math.min(point1.Position.X, point2.Position.X), math.max(point1.Position.X, point2.Position.X)},
	y = {math.min(point1.Position.Y, point2.Position.Y), math.max(point1.Position.Y, point2.Position.Y)},
	z = {math.min(point1.Position.Z, point2.Position.Z), math.max(point1.Position.Z, point2.Position.Z)},
}

local x = math.random(intervals.x[1], intervals.x[2])
local y = math.random(intervals.y[1], intervals.y[2])
local z = math.random(intervals.z[1], intervals.z[2])

-- or

local x = math.random(unpack(intervals.x))
local y = math.random(unpack(intervals.y))
local z = math.random(unpack(intervals.z))

instead of math.random() use the new method Random.new():NextInteger(a: number, b: number).

There is no strict order of numbers here, so a can be greater or less than b.

local rp = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

local point1 = script.Parent.Point1
local point2 = script.Parent.Point2

local random = Random.new()

local folder = Instance.new("Folder", workspace)
folder.Name = "StarterBalls"

local models = rp.Models:WaitForChild("Enemies")

while task.wait(.1) do

	local amount = #folder:GetChildren()

	if amount < 200 then
		local chance = random:NextInteger(1,100)
		local model
		if chance == 1 then
			model = models:WaitForChild("YellowSphere")
		elseif chance ~= 1 and chance <= 20 then
			model = models:WaitForChild("CyanSphere")

		elseif chance > 20 and chance <= 50 then
			model = models:WaitForChild("SpikedSphere")
		else
			model = models:WaitForChild("RedSphere")
		end

		local clone = model:Clone()

		if clone.Name == "SpikedSphere" then
			debris:AddItem(clone, 60)
		end

		clone.Parent = folder
		local x = random:NextInteger(math.round(point1.Position.X), math.round(point2.Position.X))
		local y = random:NextInteger(math.round(point1.Position.Y), math.round(point2.Position.Y))
		local z = random:NextInteger(math.round(point1.Position.Z), math.round(point2.Position.Z))

		clone.Sphere.Position = Vector3.new(x,y,z)

	end
end
1 Like

I got a question, is math.clamp() an option?

1 Like

Instead of random:NextInteger(), have you tried the math.random() function?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.