Debounce: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
161 bytes removed ,  27 January 2011
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.


{{CodeExample}}
{{Example|<pre>
{{LineHighlight|code=enabled = true}}
function onTouch(hit)
 
if not enabled then return end
function OnTouch(hit)
    enabled = false
{{LineHighlight|code=<nowiki>    if not enabled then return end</nowiki>}}
{{LineHighlight|code=<nowiki>    enabled = false</nowiki>}}
     print("Button pressed")
     print("Button pressed")
     wait(1)
     wait(1)
     print("Hi :D")
     print("Hi :D")
{{LineHighlight|code=<nowiki>    enabled = true</nowiki>}}
    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.
Anonymous user

Navigation menu