Let's say you have a touched event that makes a given object to do something for a substantial amount of time (that is, not instantly) such as transparency fade to invisible, then back.
EXAMPLE
01 | Object 1 = script.Parent |
05 | Object 1. Transparency = Object 1. Transparency + . 1 |
09 | Object 1. Transparency = Object 1. Transparency - . 1 |
14 | Object 1. Touched:connect(onTouched) |
Yet anytime a BasePart touches Object1, it will run the function, even if the function is already running.
So if you print the resulting Transparency of Object1, the Output will go something like this:
.1
.2
.3
.4 -- What if you touched Object1 at this point?
.1 -- The function starts over.
.2
.3
In most cases, you would want to avoid that. And to do that, you will use a debounce.
04 | Object 1 = script.Parent |
08 | if debounce = = false then |
11 | Object 1. Transparency = Object 1. Transparency + . 1 |
15 | Object 1. Transparency = Object 1. Transparency - . 1 |
22 | Object 1. Touched:connect(onTouched) |
What the 'if' statement is basically saying when...
debounce
is set to false
:
debounce
is set to true
:
EXAMPLE 2
If you want to have a part (Object2) to change it's color in a certain pattern every time it's touched, but want all three colors to appear each time.
02 | Object 2 = script.Parent |
04 | function Example 2 Function() |
05 | if debounce = = false then |
07 | Object 2. BrickColor = BrickColor.new( "Bright red" ) |
09 | Object 2. BrickColor = BrickColor.new( "Bright green" ) |
11 | Object 2. BrickColor = BrickColor.new( "Bright blue" ) |
17 | Object 2. Touched:connect(Example 2 Function) |
Regarding to your question about the for i = 1, 10
, also known as the...
for Loop
It is a very convenient tool with a pretty substantial concept to wrap around.
A 'for' loop relays a given set of statements for a period amount of time, simply.
A 'for' loop's conditional statement consists of an initialized variable that equals to 2 - 3 parameters.
for [variable] = [initialnumber], [finalnumber], [step] do
initialnumber: The first loop the 'for' loop will run. The first parameter.
finalnumber: The last loop the 'for' loop will stop at. The second parameter.
step: The increment that the loop will go by. (E.g. if it's 2, then the loop will go by 2.) The third parameter (optional; if not mentioned, then the loop assume the step is '1').
The variable doesn't have to be strictly i
. It is, of course, a regular variable and can be named as almost whatever you want.
'for' loops are instantaneous, which is why people add a 'wait()' function to pause the thread enough to view the intended effects.
EXAMPLE
OUTPUT:
2
4
6
8
10
&c until it reaches 500.
For more information about the 'for' loop, visit here.