Error: bad argument #2 to 'random' (interval is empty)

Hi. I have no idea why I am getting this error. It happens in the first line in the last function ‘goalPos’. Anyone know why?

local size = 20
local scale = 2

local grid = Instance.new("Model", workspace)
grid.Name = "grid"

local tiles = {}

for x=1, size do --generate grid
	for y=1, size do
		local tile = Instance.new("Part")
		tile.Name = (x .. "," .. y)
		tile.Position = Vector3.new(x*scale, 1, y*scale)
		tile.Size = Vector3.new(scale, .1, scale)
		tile.Anchored = true
		tile.BrickColor = BrickColor.new("White")
		tile.TopSurface = "Smooth"
		
		local outline = Instance.new("SelectionBox", tile)
		outline.Adornee = tile
		outline.LineThickness = .01
		outline.Color3 = Color3.new(0,0,0)
		
		if x == 1 or y == 1 or x == size or y == size then --give border
			tile.BrickColor = BrickColor.new("Black")
		end
		
		tile.Parent = grid
		
		table.insert(tiles, {x, y, tile})
	end
end

function getTilePart(x,y) --get a physical object based on coord
	for k,v in pairs(tiles) do
		if x == v[1] and y == v[2] then
			return v[3]
		end
	end
end

function startPos() --pick random starting point near bottom left
	local x = math.random(2,5)
	local y = math.random(2,5)
	local tile = getTilePart(x,y)
	tile.BrickColor = BrickColor.new("Pastel Blue")
end
startPos()

function goalPos() --pick random goal near top right
	local x = math.random(size-1,size-6)
	local y = math.random(size-1,size-6)
	local tile = getTilePart(x,y)
	tile.BrickColor = BrickColor.new("Pastel Blue")
end
goalPos()
13 Likes

Your upper limit is less than your lower limit, so there are effectively no values between them.

48 Likes