Beginning: To create a GUI button to work once clicked, you will need either a script or local script.
I personally believe the local script is better because it only shows to the player who clicked it. Unlike the script, it happens to everyone in the server.
How to begin:
The first thing I like to do when making a GUI button to work, is starting a function. This function will help the script detect when a person clicks a button. For example, this function for me would look like:
function onClick()
That will start the script, but if you have any important variables, I recommend putting them before you start the function.
Closing the function:
To close the function, you need to add a line of code beneath the end. When I say beneath the end, I mean:
function onClick()
end
-- Line of code here
You don't just put any code there, you have to put where the script is targetting and if a click is required. So it would look like:
function onClick()
end
script.Parent.MouseButton1Click:Connect(onClick)
We basically just told the script that we are connecting the button we are apart of, and once clicked 1 time the script will take place.
After that, we have the connection to the function. That is why we had to put 'onClick' in the brackets of the connection.
Adding a purpose:
Once we understand all this, we can now do the last thing required. We can add the purpose of the button. The purpose of the button is for something to happen once you click it. In this case, we are just going to print that is was successfully clicked.
function onClick()
print("This button was clicked!")
end
script.Parent.MouseButton1Click:Connect(onClick)
Output:
This button was clicked!
Where the print is located, you can add whatever you want to happen to get the script to work as you would like.
Conclusion:
We successfully made a function, to do our code once the button is clicked with an ending connection back to the function. Of course there are other ways you can do this, but this way is the easiest for me that I find unique. I hope you enjoyed this tutorial!