Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I check if the mouse CFrame is inside a region3 CFrame?

Let's say you can only build inside a certain restricted area, how would I check if the mouse is inside that area so that you can place in there? I tried this:

spawn(function()
mouse.Move:Connect(function()
	if mouse.Hit.p < region.CFrame.p and region.Hit.p < mouse.CFrame.p then
		print("yep")
	end
	end)
end)

But errored that I can't compare 2 userdata values.

1 comment

0

these can never be smaller than each other, one of them has to be bigger and the other one smaller

1 answer

Since you can't compare userdata with each other, you'll have to compare each individual axis with each other instead.

like such:

-- This function will take two vector as arguments and
-- compare break it down to axis and then compare
-- each axis value with each other.
function compareVectors(v1,v2)
	local x1,y1,z1 = v1.x, v1.y, v1.z
	local x2,y2,z2 = v2.x, v2.y, v2.z
	return x1 < x2 and y1 < y2 and z1 < z2
end

spawn(function()
	mouse.Move:Connect(function()
		local check1 = compareVectors(mouse.Hit.p,region.CFrame.p)
		local check2 = compareVectors(region.Hit.p,mouse.CFrame.p)
		if check1 and check2 then
			print("yep")
		end
	end)
end)


This discussion is preserved as it was. New answers, comments, and votes are closed.