Functions In Roblox Development

·

·

Welcome back to my Roblox beginner scripting tutorial guide. In this episode, we’ll be discussing functions.

This is another fundamental concept in scripting that you’ll use frequently inside your scripts. Let’s dive in.


Setting Up the Environment

On the right side, disable the “property” script we created in the last episode. Select it and click Disable.

Now click the plus sign next to Workspace, search for Script, and insert one. Right-click the script and rename it to:

Functions

Then delete any default code inside the script.


What Is a Function?

Before I explain what a function is, let me give you a scenario.

Let’s say I want to take the Baseplate and repeatedly change one of its properties — like Transparency.

First, make a reference to the Baseplate:

local myBaseplate = game.Workspace.Baseplate

Now let’s change its Transparency:

myBaseplate.Transparency = 1
myBaseplate.Transparency = 0.5
myBaseplate.Transparency = 0

If I wanted to repeat that again, I’d have to copy and paste those three lines. Do that a few more times and suddenly we’ve got 6, 9, or more lines doing the same thing — not efficient.


Enter: Functions

What if I told you there’s a better, cleaner way to do that?

A function is a block of code that runs just by calling it once — no need to repeat yourself.

Let’s undo our repetitions (you can press Ctrl + Z) and go back to those three lines of code.

Now let’s define a function right after our Baseplate reference:

local function changeTransparency()
myBaseplate.Transparency = 1
myBaseplate.Transparency = 0.5
myBaseplate.Transparency = 0
end

To run this function, just type:

changeTransparency()

This one line will execute all the code inside the function. If we want to call it again, just write that same line.


Make the Changes Visible

By default, the Baseplate’s Transparency is already 0. So to see the effect clearly, let’s add some lines before and after:

myBaseplate.Transparency = 1
myBaseplate.Transparency = 0.5
myBaseplate.Transparency = 0

Now when we play the game, we’ll see the Baseplate go transparent and back.

You can now call changeTransparency() as many times as you want. Each time, it performs the same sequence.


Equivalent Code, Better Organized

This one function call:

changeTransparency()

is equivalent to copying and pasting those five lines over and over again.

This is a cleaner and more optimized way to write code — especially useful for larger scripts.


Second Example: Print A, B, C

Let’s delete our current script content (Ctrl + A, then Backspace) and try another example.

We’ll make a function that prints three letters:

local function printABC()
print("A")
print("B")
print("C")
end

printABC()
printABC()
printABC()

When you run this, you’ll see “A”, “B”, and “C” printed three times in the Output window.


Practice Exercise

For your learning objective, I want you to create more functions that do different things.

Some ideas:

  • Take a part and change its material
  • Modify another property like color or size

Try it, then share your code in the comments to show others what you’ve learned.


Wrap-Up

That concludes this walkthrough on functions. I hope it helped you understand how they work and why they’re important.

Thanks for reading, and I’ll see you in the next one. Take care.