I'm making a button that will play music when you click it. All I need to know is how to trigger the event when you click the part.
MouseClick checks the part your mouse clicks, to do this > you must insert the object ClickDetector
in the part.
Script in part:
1 | local part = script.Parent --Define Parent |
2 | local cd = part.ClickDetector --Define ClickDetector |
3 | local sound = part.Sound --Define Sound |
4 |
5 | cd.MouseClick:connect( function () --Connect function to MouseClick |
6 | sound:Play() --Play Sound |
7 | end ) -- End Function |
ClickDetector: http://wiki.roblox.com/index.php?title=API:Class/ClickDetector MouseClick: http://wiki.roblox.com/index.php?title=API:Class/ClickDetector/MouseClick
Though this isn't a request site, I can give you the basics.
When you're making a GUI button, you need to just put a localscript, inside of it. If this is the case of the localscript being inside the TextButton, follow this code.
1 | script.Parent.MouseButton 1 Click:connect ( function (Clicked) -- Button was clicked fully |
2 | print ( "The button with the name of " ..script.Parent.Name.. " was clicked." ) -- Print a confirmation |
3 | end ) -- End the function |
This code basically functions with putting the text inside the parenthesis, or the ()'s, when clicked by the Left Mouse Button, or MouseButton1. This would be the setting stone to starting the music script. You should note that the Click, and Down functions are different, click means the button was down then up. Down means it was just pushed down.
If this helped you, please click the Accept Answer button, thank you.