Scripting Book/Chapter 2

From Legacy Roblox Wiki
Jump to navigationJump to search

Introduction

Hi, welcome to chapter 2 of my scripting book! This time, now that we've covered all the basics, we're going to move onto something important.

We're going to start with creating our own functions! Then we're going to go a bit more advanced and learn how to connect them to an Instance.

Creating our own functions

This subpage will teach us about creating our own functions.

What is a function?

A function is used to activate code and to keep your script tidier.

Ways of creating a function

There are two ways of creating named functions. Local functions,

local function = hello()

end

and normal functions.

function hello()

end

In the examples above, 'function' tells the script we are going to start coding a function, 'hello' is the name of the function, it can be anything you want. The two brackets are where arguments go, but we're going to make simple functions for now. So forget about arguments. The 'end' tells the script where the function's source stops, otherwise the function would never stop, and error.

Making message functions

Let's make a message function! What we're going to do is create a function, and when you call the function (we will learn how to call functions later), a message will pop up on the screen saying 'Howdy, y'all!'.

I'm going to make a normal function and call it msg.

function msg()

end

In the source, I'm going to use a variable to hold an Instance, which will be the message and a parent in workspace.

function msg()
    m = Instance.new("Message", game.Workspace)

end

'm' is my variable's name, so whenever we change its 'Text' property we need to use 'm'. Let's change the message's (or m's) text property to 'Howdy, y'all!'.

function msg()
    m = Instance.new("Message", game.Workspace)
    m.Text = "Howdy, y'all!"
end

Remember, 'Howdy, y'all!' is a string because it contains text, so we need to surround it in two friendly speechmarks.

Next, we need to add a wait - does what it says on the tin. 'wait' is a function, so it contains one argument which is a number. So wait(5) would wait 5 seconds and then continue with the following code.

After those 5 seconds, we need to remove the message, otherwise that annoying message will be on our screen forever! Yuck! In order to remove this annoying message, we use the variable's name and the :Remove() function to delete it completely.

function msg()
    m = Instance.new("Message", game.Workspace)
    m.Text = "Howdy, y'all!"
    wait(5)
    m:Remove()
end

Congratulations! You have created your first function!

Calling a function

What does it mean to call a function? It means to activate the function. Before it is 'called' (or activated), it is invisable to the script, so it is not executed (or ran).

How do I call a function? It's pretty simple actually. All we need to do is, make sure we are outside the function and say the function's name, and the two brakets - and the arguments if we have any, which we don't. So to call our 'msg' function, all we need to do is write the following:

msg()

So now, when we press 'Run' in ROBLOX Studio, a message will pop up on the screen saying 'Howdy, y'all!' for 5 seconds then it will disapear. Cool, isn't it!?

Using ROBLOX functions

Here's an unfinished list of ROBLOX functions:

:Remove()
:IsA(className)
wait(time)

:Remove() sets the parent of an object to nil (deletes it.)

:IsA(className) checks if the object is a certain Instance.

wait(time) halts the script for TIME seconds before continuing.

Connecting a function

What does it mean to connect a function? Well it fires the function when something is done to an object.

You can change what makes the function fire by changing the event. We just create a normal function (as we did before), but instead of calling it, we're going to add a 'connection' line at the bottom of the script.

For example, if we put this in a WedgePart,

function onTouched()
    m = Instance.new("Message", game.Workspace)
    m.Text = "Ouch! That hurts!"
    wait(5)
    m:Remove()
end

script.Parent.Touched:connect(onTouched)

Whenever you touch the parent of this script, a message will appear saying "Ouch! That hurts!", haha, neat huh?

The function is just the same, the function name is always editable. But whenever you change the function's name, you have to change the text in the two brakets in the connection line to the function's name.

In the connection line above, 'Touched' is the event and the :connect() function connects the function to the event, therefore creating a 'call' or 'fire'.

Go to previous chapter or Continue to Chapter 3