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

How to script a button?

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.

1 comment

0

This is not a request site.

2 answers

MouseClick checks the part your mouse clicks, to do this > you must insert the object ClickDetector in the part.

Script in part:

local part = script.Parent --Define Parent
local cd = part.ClickDetector --Define ClickDetector
local sound = part.Sound --Define Sound

cd.MouseClick:connect(function() --Connect function to MouseClick
	sound:Play() --Play Sound
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.

script.Parent.MouseButton1Click:connect (function(Clicked) -- Button was clicked fully
	print("The button with the name of "..script.Parent.Name.." was clicked.") -- Print a confirmation
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.

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