How to find empty space between parts?

I have a couple of parts in a folder and a hole between the parts. I want to find the center of the hole (position / cframe)

Problems:

  • can’t find center of hole
  • only searches diagonally
  • only works when model is positive x and positive z on position

local PartsFolder = game:GetService("Workspace"):FindFirstChild("Floors"):FindFirstChild("Floor1")
print(PartsFolder, PartsFolder:GetExtentsSize())

print((PartsFolder:GetPivot().X - ((PartsFolder:GetExtentsSize().X/2))), (PartsFolder:GetPivot().X + ((PartsFolder:GetExtentsSize().X/2))))
local areaX = math.floor(((PartsFolder:GetPivot().X + ((PartsFolder:GetExtentsSize().X))))) - 8

for i = 4, areaX, 1 do
	local rayOrigin = Vector3.new((PartsFolder:GetPivot().X - ((PartsFolder:GetExtentsSize().X/2)) + i), PartsFolder:GetPivot().Y + 4, (PartsFolder:GetPivot().Z + ((PartsFolder:GetExtentsSize().Z/2)) - i))
	local rayDirection = Vector3.new(0, -8, 0)

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	print(raycastResult)
	if raycastResult == nil then
		print("hole position: ".. tostring(rayOrigin))
		game.Players.LocalPlayer.Character:PivotTo(CFrame.new(rayOrigin))
		break
	else
		raycastResult.Instance.BrickColor = BrickColor.new("Maroon")
	end
end

Current:
Screenshot_6

What I want:
Screenshot_8

Simply put a part in the hole and find the center of the part

No for my use case it has to be done through scripting, I would’ve just done that if I could obviously.

Why can’t you do that? I don’t know how to do that trough scripts but what I would do is check the 2 coordinates needed and find the distance between them

The model of parts is randomly generated

Oh then I don’t have any idea of how to do that

I don’t really have much knowledge on this subject but I suggest using raycast.

Yea I used raycasts but I don’t know how to make it search the whole thing, got it searching diagonally though

I think you could be able to raycast from a part in the sky, then find the part and etc. (Hope you know what I mean)

Like I said before I can’t place parts and the floor is randomly generated

Well, you’re only looping over the x coordinates. You’ll need a nested loop that visits every combination of x and z coordinates.

Something like

for x = 4, areaX do
    for Z = 4, areaZ do
        local rayOrigin = Vector3.new(x, 0, z)
        ...
    end
end

Thanks to these devforum posts:

Solution:

repeat task.wait() until game:GetService("Workspace"):FindFirstChild("Floors", true)
local MODEL = game:GetService("Workspace"):FindFirstChild("Floors", true):FindFirstChild("Floor1")
local HEIGHTPART = MODEL:FindFirstChildWhichIsA("BasePart")
local SIZE = MODEL:GetExtentsSize()
local CENTER = MODEL:GetPivot()

local VERTICES = {
	{1, 1, -1},
	{-1, 1, 1}
}

local BOUND1 = (CENTER * CFrame.new(SIZE.X/2 * (VERTICES[1])[1], SIZE.Y/2 * (VERTICES[1])[2], SIZE.Z/2 * (VERTICES[1])[3])).Position
local BOUND2 = (CENTER * CFrame.new(SIZE.X/2 * (VERTICES[2])[1], SIZE.Y/2 * (VERTICES[2])[2], SIZE.Z/2 * (VERTICES[2])[3])).Position

local BOUNDS = {
	Vector2.new(BOUND1.X, BOUND1.Z),
	Vector2.new(BOUND2.X, BOUND2.Z - 4)
}


local ROWS = 24
local COLS = 24

local GROUND = HEIGHTPART.Position.Y
local HEIGHT = 8

local LINE_COLOR = Color3.fromRGB(156, 54, 89)
local LINE_THICKNESS = 0.1
local LINE_TRANSPARENCY = 0.15

local holePositions = {}

local function castRayGridNormal()
	local corner1 = BOUNDS[1]
	local corner2 = BOUNDS[2]

	local boundIntX = (corner2.X - corner1.X) / ROWS
	local boundIntZ = (corner2.Y - corner1.Y) / COLS

	for i = 1, ROWS do
		for j = 1, COLS do

			local x = corner1.X + boundIntX * i
			local z = corner1.Y + boundIntZ * j

			local rayParams = RaycastParams.new()
			local raycast = workspace:Raycast(Vector3.new(x, GROUND + HEIGHT, z), Vector3.new(0, -14, 0),  rayParams)
			print(raycast)
			if raycast == nil then
				table.insert(holePositions, Vector3.new(x, GROUND + HEIGHT, z) + Vector3.new(0, -14, 0))
			end
		end
	end
end

castRayGridNormal()

local minX, maxX = math.huge, -math.huge
local minY, maxY = math.huge, -math.huge
local minZ, maxZ = math.huge, -math.huge

for _,v in pairs(holePositions) do
	if (v.X < minX) then minX = v.X end
	if (v.X > maxX) then maxX = v.X end
	if (v.Y < minY) then minY = v.Y end
	if (v.Y > maxY) then maxY = v.Y end
	if (v.Z < minZ) then minZ = v.Z end
	if (v.Z > maxZ) then maxZ = v.Z end
end

local minVector = Vector3.new(minX, minY, minZ)
local maxVector = Vector3.new(maxX, maxY, maxZ)

local centerOfHole = minVector:Lerp(maxVector, 0.5)
print(centerOfHole)

Yes but why can’t you place parts in the sky? If you would place it in the sky there would be no problem at all, make it way bigger than the floor and then you can rayvast getting what you need

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