1 | function onClicked() |
2 | wait( 1 ) |
3 | script.Parent.GUIAUDIO:play() |
4 | wait( 1 ) |
5 | end |
6 |
7 | script.Parent.MouseConnect:connect(onClickedButton) |
--This is what i have so far
Assuming the script's parent is a GUI button, you can use MouseButton1Click as the event to trigger a function to occur.
What is MouseButton1Click and how do I use it in my script?
MouseButton1Click is an event in a GUI button that is triggered when the left mouse button is clicked. So by assuming that the script's parent is a GUI button, you can change your script to this:
1 | function onClicked() |
2 | wait( 1 ) |
3 | script.Parent.GUIAUDIO:play() |
4 | wait( 1 ) |
5 | end |
6 |
7 | script.Parent.MouseButton 1 Click:connect(onClicked) --The function name should be in the parenthesis (brackets) of the connect method. |
But what if your GUI button is a child of the script's parent? Well, all you need to do is change the path on the connection line so that it points to your GUI button.
1 | function onClicked() |
2 | wait( 1 ) |
3 | script.Parent.GUIAUDIO:play() |
4 | wait( 1 ) |
5 | end |
6 |
7 | script.Parent.BUTTON.MouseButton 1 Click:connect(onClicked) --Change BUTTON to the name of your GUI button (TextButton or ImageButtons are GUI buttons). |
Shortening your script:
While the script I provided above will work, you can shorten your code so that it performs better. You can use an anonymous function to make your script shorter.
1 | script.Parent.MouseButton 1 Click:connect( function () |
2 | wait( 1 ) |
3 | script.Parent.GUIAUDIO:play() |
4 | wait( 1 ) |
5 | end ) |
Read more on anonymous functions here.
MouseConnect
is not a valid event. If you're not sure about properties, events, etc. always be sure to look it up on the Roblox wiki. The proper event for TextButtons is MouseButton1Click
.
Another problem is that the function you try to connect the event to doesn't exist. You named your function onClicked
, but you try to connect the event to a function named onClickedButton
. This will result in an error. You need to do something like:
1 | script.Parent.MouseButton 1 Click:connect(onClicked) |
Since I don't know the hierarchy, it's possible that MouseConnect
is a GUI and you forgot an event entirely. If this is true, you just have to add the event:
1 | script.Parent.MouseConnect.MouseButton 1 Click:connect(onClicked) |