We use cookies on this site to enhance your user experience

Use MouseClick on Parts

Use MouseClick on Parts

Jun 23 2018, 2:01 AM PST


You want to create a part that, when clicked, shows a message.


Use the MouseClick event and the ClickDetector|ClickDetector object.

local part, clickd, msg = Workspace.Part, Instance.new('ClickDetector'), Instance.new('Message')
clickd.Parent = part
msg.Text = "Clicked!"
	msg.Parent = Workspace
	msg.Parent = nil


The variable declaration statement looks a tad different this time. Here we’re using a multiple assignment which is just syntactic sugar (syntax shortcuts used to save time or make something more readable) for just three normal variable declarations. Make sure you have a part in the Workspace named “Part”. Notice we create a new instance of a ClickDetector which is just an object that controls clicks to its parented object. We parent it to the part variable (since we want to record clicks to that object) and we also set the message’s text.

We then connect the ClickDetector/MouseClick|MouseClick event of the ClickDetector, this obviously fires once the ClickDetector's parent is clicked. Inside the callback we just parent the message to the Workspace, wait 2 seconds and remove it.

You may be wondering why MouseClick requires an entirely new object in order for you to be able to record clicks a part. Why couldn’t it be like the Touched event and be part.MouseClick? It is probably because, when you put a ClickDetector in a part, putting your cursor over it makes the cursor’s image change. Usually, you’d only want that to happen with parts you can click. It was likely an API design choice made by the developers.