Hello guys!

Im looking to figure out how i can get the closest "attachment" to my mouse, basically i have a ray that detects which part im looking at and i have a floor with 4 attachments i wanna get the closest attachment to my mouse. Here is my current code. Its not working!

local function GetAttachment(x,y)
	local nearestAttachment = nil
	local target = mouse.Target
	for i,v in pairs(hit:GetChildren()) do
		if v:IsA("Attachment") then
			local mag = (mouse.Hit.p - hit.Position).magnitude
			if mag < 2 then
				print(v.Name)
			end
		end
	end
end

Basically "hit" is the object the ray is touching! Thanks again!

2 comments

0

Use game.RunService.RenderStepped():Connect()

0

For what though? The function is already in a renderstep.