First Lua Functions

What is a Function?

Sometimes you will have complex activities that will require multiple lines of code. Rather than repeating lots of lines of code, you can store it all in a function. You type function then the name of it and then two parentheses.

Type this into a script, then run the game:

function party()
print(“Let’s have fun!”)
print(“Join the server.”)
print(“Awesome!”)
end
party()
party()

Touch Part Function

There’s lots of times in Roblox that you have to touch a block to win or something can hurt you if you touch it.  Delete anything you have in the game. Place a Part in and make it float in the air by checking anchored. Make a child script to the Part that does the following:

function brickTouch()
print(“Hey, you are touching me!”)
end
script.Parent.Touched:connect(brickTouch)

This is another reason to use functions. You can call them when certain things happen. “script.Parent.Touched” is a code that checks if the parent of an object has been touched = the floating part. “connect(brickTouch)” tells the code that if the Part has been touched, call the function brickTouch.

Another way to call a function is to list it as it happens. It works the same if you write it like this.

script.Parent.Touched:connect(function()
print(“Hey, you are touching me!”)
end)

That’s easier usually if you only plan to call that function once and don’t want to give it a name.

Prevent Looping with a Debounce

You’ll notice that if you touch the Part, it calls the function many times. That’s because the game is updating every single frame. Several pieces of your player may touch that Part. But we only want it to detect a touch once every two seconds. Let’s build it into the code with a Boolean!

Add the bold words to the code:

touching = false
function brickTouch()
if touching == true then return end
touching = true
print(“Hey, you are stepping on me!”)
wait (2)
touching = false
end

script.Parent.Touched:connect(brickTouch)

What this does is uses “touching” to check if you are touching the part. If you are not, it runs the code. If you are touching, it makes you wait 2 seconds before you can touch it again.

Play the game. 

The code is checking with the Boolean “touching.” If touching is true, it ends the function so you don’t loop through again. This is a debounce.

If you touch it for the first time, it sets touching from false to true and runs the code, making the game wait 2 seconds before it turns touching back to false.

Changing Color to Show True or False

Let’s show touching on the block by changing its color. This Part doesn’t like to be touched. When touching is true, we will turn it red. This will help you see how the debounce works.

touching = false
script.Parent.BrickColor = BrickColor.Blue()

function brickTouch()
if touching == true then return end
touching = true
print(“Hey, you are stepping on me!”)
script.Parent.BrickColor = BrickColor.Red()
wait (2)
touching = false
script.Parent.BrickColor = BrickColor.Blue()
end

script.Parent.Touched:connect(brickTouch)

QuizzesStatus