Scripting Tutorial #3 - Functions

Links to previous tutorials:

Tutorial #1 - Printing
Tutorial #2 - local Variables

Welcome back to another tutorial!

Today, we will cover functions and how we can use them in a script.

Functions can be used to release parts of scripts in a series of steps, unlike with a normal script which does it all in one go.

I will cover how to use it and how it works:

Example:

Make a new LocalScript in StarterPlayerScripts (inside StarterPlayer)
Copy and paste the below code:

function PrintPlayerName()
local Player = game.Players.LocalPlayer
print(Player.Name) 
end

wait(5)
PrintPlayerName()

If after 5 seconds you see your username on Output or Dev Console, you successfully made a function!

Let’s say we have two functions…

function Print(text)
print(text)
end

function Warn(text)
warn(text)
end

You can choose which ones go first, like print() first or warn() first.

function Print(text)
print(text)
end

function Warn(text)
warn(text)
end

Warn("Hello world!")
wait(3)
Print("waffle")

Thanks for reading this post! :slight_smile:

Next tutorial, we will cover InsertService

4 Likes

free coding class i guess :man_shrugging:

3 Likes

Gonna put this in off topic because it’s not about cc2

4 Likes