You can use almost the exact same code inside the button.
You don't need the very first "function" or the "end" for the function.
Everything else in between those lines will be fine.
Attached is a picture highlighting a button that I am using for testing.
Select the button
Notice on the left the little square with the lightning bolt, select that. This is where you can put your code. There are several options.
I usually put code in the "Clicked Script". Normally that will suffice. But you can look at the clicked actions and see what you can do without any code as well.
Also, it looks like you're turning on an output. You can have another button turn that output off, or use the same button to toggle the output on and off with some more code.