Please explain and provide and example script such as one to prevent you from stepping on the same brick to many times. I have read the forum and do not understand the exact scripting property's of this function
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
Object1 = script.Parent function onTouched() for i = 1, 10 do Object1.Transparency = Object1.Transparency + .1 wait() end for i = 1, 10 do Object1.Transparency = Object1.Transparency - .1 wait() end end Object1.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.
-- Static Variables debounce = false -- You can name 'debounce' as anything, because it is a boolean value. The CONCEPT itself is called 'debouncing'. Object1 = script.Parent -- function onTouched() if debounce == false then -- What I just created here is essentially a 'lock' that ensures that the function is fired only ONCE. debounce = true -- Now the 'if' statement can't run again until debounce is reverted back to 'false'. for i = 1, 10 do Object1.Transparency = Object1.Transparency + .1 wait() end for i = 1, 10 do Object1.Transparency = Object1.Transparency - .1 wait() end debounce = false end end Object1.Touched:connect(onTouched)
What the 'if' statement is basically saying when...
debounce
is set to false
:
if false == false then -- The script will then run.
debounce
is set to true
:
if true == false then -- The script will NOT run, therefore skipping over the 'if' statement.
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.
debounce = false Object2 = script.Parent function Example2Function() if debounce == false then debounce = true -- You can say I've 'turned off' the 'if' statement until line 13 Object2.BrickColor = BrickColor.new("Bright red") wait(.5) Object2.BrickColor = BrickColor.new("Bright green") wait(.25) Object2.BrickColor = BrickColor.new("Bright blue") wait(.25) debounce = false end end Object2.Touched:connect(Example2Function)
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
for Tobu = 0, 500, 2 do -- 'Tobu' is the initialized variable with '0' as the starting number and '500' as the ending number, while going up by '2'. print(Tobu) -- This will print the variable 'Tobu' in the output. end
OUTPUT:
2
4
6
8
10
&c until it reaches 500.
For more information about the 'for' loop, visit here.