So I have ascript.Parent.Touched:Connect(function(hit)
function that makes a block fade in, but it keeps on running even if it's in the middle of running. I want it to only run once and then keep the block visible. How would I do this?
Debounce! Debounce doesn't allow a function to run again until a set time has passed.
My example of Debounce;
01 | local Part = script.Parent |
02 | local Debounce = false |
03 |
04 | Part.Touched:Connect( function (hit) |
05 | if Debounce = = false then |
06 | Debounce = true |
07 | print ( "This cannot run again until 5 seconds have passed!" ) |
08 | wait( 5 ) |
09 | Debounce = false |
10 | end |
11 | end ) |
If you want it to run only ONCE, then use connections and disconnect them.
1 | local Part = script.Parent |
2 | local Connection |
3 |
4 | Connection = Part.Touched:Connect( function (hit) |
5 | --Code |
6 | Connection:Disconnect() |
7 | end ) |
1 | function onTouched(hit) |
2 | -- code |
3 | connection:Disconnect() |
4 | end |
5 |
6 | local connection = script.Parent.Touched:Connect(onTouched) |
or
1 | script.Parent.Touched:Wait() |
2 | -- code |