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

How can I identify the side of an object the mouse's click was?

I need get the distance from the center of an object to the mouse click position, I can, but if I go to the other side of the object the value turns negative, and I need identify if the click was at left or right of the object's center, so I cant use the value signal to do this, I hope you can help me: image

I'm using this to get the value

mouse.Hit:toObjectSpace(object.CFrame).p.X

1 answer

You can use the math function math.abs() to construct a new Vector3 from the absolute value of each components of the p vector and then retrieve it's X component:

local tool = script.Parent


local function onButton1Down(mouse)
	local part = mouse.Target
	
	-- Just to make sure the mouse isn't pointing nowhere or at the baseplate
	if part and part.Locked == false then
		local vec = mouse.Hit:toObjectSpace(part.CFrame).p
		
		print(Vector3.new(math.abs(vec.x), math.abs(vec.y), math.abs(vec.z)).X)
	end
end

local function onEquipped(mouse)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end


tool.Equipped:connect(onEquipped)

Hope this helps. If it did, please leave an upvote and mark my question as answered. :) If you need anything else, leave me a message.

2 comments

0

Thanks for your answer, but this does'nt solved.. please look the image link, let me try explain in another way my english is not so good...I couldn't figure out what is going on the Part's local space, if I click on it after 2 studs from the its center/origin, the X axis says 2.0 but if I click on the same point again but with my character on its other side it says -2, like the X axis has inverte

0

Oh, I see. I'm sorry, I thought you meant it turned negative if you clicked at the other side of part on the same axis, if it makes sense. I've been trying to figure out how to fix it for two hours now, but still nothing. I'm not particularly good with Vector and CFrame math. :P

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