Get closest edge of part to mouse pointer?

Hello all,

I’m in the process of designing an editor for an upcoming game where the player can build whatever they want to. What I’m trying to achieve is a wall editor where the wall will position itself along a selected edge, which means you don’t have to rotate the walls yourself.

For example, Retail Tycoon uses this system for its walls. They align along the closest edge to the mouse.

How can I get the closest edge of a part to the mouse and then position the wall on it?

I first thought of the idea of using multiple parts to represent the edges and the center, but this doesn’t seem like a good way to do it especially since there are ~ 1000 grid spaces, which would each have 5 parts = 5000 parts per plot (not very good)

The grid spaces are each 8 x 0.5 x 8, no rotation

Thanks in advance!

You can get the part using Mouse.Target or by using raycasting after that you get all edges of that part by doing some cframe math

local Edge1 = Part.CFrame * CFrame.new(Part.Size.X * 0.5,Part.Size.Y * 0.5,0)
– make other edges

then you can turn that into a table of edges and use a loop and magnitude to determine which one closer

if you don’t want to use magnitude you can do more local space math (point collision) to determine whats closer

1 Like

Ok, thanks. I’ll experiment with the magnitude method first, see what happens

Ok, I got it to work with the left and right sides, thanks! Now I just have to do front and back and then get the lowest distance from them all. I originally tried to simply put them in a table with this format {Left = (mouseP - edgeP).magnitude} and then do table.sort but I haven’t really experimented much with tables.

Any way I can sort a table of tables (array), or a different method?

There are many ways of sorting tables of tables, you could look into:

table.Foreach
table | Documentation - Roblox Creator Hub

For Loops Are also optional

1 Like

I don’t believe so you probably have to make your own sorting for that I would just use a brute force method to determine whats closer

If your sorting a table of distances I guess you could do

tbl.sort(distancetbl)

1 Like

Yeah, its just I need to work out which distance corresponds to which edge, because a table returning {1, 2, 3, 4} doesn’t tell me which edge is which. I’ll probably go with @Jaycbee05 and make my own sort function

It can doe?
local tbl = {}
local distances = {}
local distance
local edge

tbl[distance] = edge
distances[#distances + 1] = distance
tbl.sort(distances)
return tbl[distances[1]]

– would probably want to round numbers if doing this method

I got a method that sorts the array (I had to look it up as I got confused) however all it outputs is nil:

local function sortArray(dict)

local array = {}

for key, value in pairs(dict) do
	array[#array+1] = {key = key, value = value}
end

table.sort(array, function(a, b)
	return a.value > b.value
end)

return dict

end




local function gCE()

Table = {
	
	['Left']  = (m.Hit.p - left.p ).magnitude,
	['Right'] = (m.Hit.p - right.p).magnitude,
	['Front'] = (m.Hit.p - front.p).magnitude,
	['Back']  = (m.Hit.p - back.p ).magnitude
	
	}

Table2 = sortArray(Table)

return Table2

end

I used httpservice to read the table and it is sorting it (although its biggest to smallest rn) however the output of the first value is nil.

[{"Back":457.638458251953125,"Left":455.655426025390625,"Right":452.3565673828125,"Front":450.350341796875}]
[nil]

I print the entire table and then the first value using this: tabl = gCE() print(tabl[1])

I worked it out! Truns out what I was sorting wasn’t a table of tables, which I have now resolved. Everything works now, I’m getting the correct named side that my mouse is focused on. Thanks all!