How to find out which part is under another part?


I want find out which part is under the plate above so i can connect them for my game. How can I do that?

1 Like

You would have to use raycasting.

local plate = workspace.Plate -- Should be your actual plate. You can look through all of them for example

local rayOrigin = plate.Position - Vector3.new(0, plate.Size.Y / 2, 0) 
local rayDirection = Vector3.new(0, -100, 0) 

-- Here you wanna ignore the plate itself
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {plate}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

local result = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if result then
    local partBelow = result.Instance
    print("Part found below plate:", partBelow.Name)
end
1 Like

ive thought about that but i was a bit scared of crashing the game, would it work? there are like 200x200 parts

1 Like

In that case, if the plate and part sizes are the same (same width and length) you could give each plate and each part below a unique name based on the coordinates.

This would only work if the plate and part grid is exactly on top of each other (each plate has exactly 1 colored part below it)

E.g.

-- Naming the colored parts (when you spawn them in, or just based on their X and Z position)
part.Name = xPosition..","..zPosition

-- Naming the plates (when you spawn them in, or just based on their X and Z position)
plate.Name = xPosition..","..zPosition

local partBelowPlate = workspace.ColoredParts:FindFirstChild(plate.Name)

this might actually work, let me try

Edit - It worked, thanks!

1 Like

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