Debounce: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
no edit summary
>Camoy (formatted) |
>Camoy No edit summary |
||
Line 42: | Line 42: | ||
It's fairly simple to convert an existing script to using debounce. Lets use the same script we had above, and add a couple of lines. In this case we will put in a time limit to wait for until the function can be run again. | It's fairly simple to convert an existing script to using debounce. Lets use the same script we had above, and add a couple of lines. In this case we will put in a time limit to wait for until the function can be run again. | ||
{{ | {{Example|<pre> | ||
function onTouch(hit) | |||
if not enabled then return end | |||
function | enabled = false | ||
print("Button pressed") | print("Button pressed") | ||
wait(1) | wait(1) | ||
print("Hi :D") | print("Hi :D") | ||
enabled = true | |||
end | end | ||
game.Workspace.Button.Touched:connect(OnTouch) | game.Workspace.Button.Touched:connect(OnTouch) | ||
</pre> | </pre>}} | ||
#The first line that we added was <code style="color:blue">enabled = true</code>. This line creates a [[Variables|global variable]] called '''enabled'''. This is the ''flag'' that we will use to let the function know if it is allowed to run or not. | #The first line that we added was <code style="color:blue">enabled = true</code>. This line creates a [[Variables|global variable]] called '''enabled'''. This is the ''flag'' that we will use to let the function know if it is allowed to run or not. |