Functions: Difference between revisions

From Goodblox Wiki
Jump to navigationJump to search
mNo edit summary
 
(2 intermediate revisions by the same user not shown)
Line 9: Line 9:
<pre>
<pre>
local function myfunction() -- what this function does
local function myfunction() -- what this function does
local x = 2+2
    local x = 2+2
print(x)
    print(x)
end
end


Line 35: Line 35:
<pre>
<pre>
local function newbrick()
local function newbrick()
local p = Instance.new("Part")
    local p = Instance.new("Part")
 
   
p.Parent = workspace
    p.Parent = workspace
p.Name = "Brick"
    p.Name = "Brick"
p.Anchored = true
    p.Anchored = true
end
end


Line 45: Line 45:
</pre>
</pre>


Here, the function (called "newbrick()") is creating a new [[Instance]] of a [[Part|brick]].  That brick will be in the [[Workspace]].  We are inserting a specific object, namely a [[Part]], and naming it "Brick".  We want to [[Anchor]] it so the brick won't fall down.
Here, the function (called "newbrick()") is creating a new [[Instance]] of a [[Part|brick]].  That brick will be in the [[GBX.lua.Workspace (Service)|Workspace]].  We are inserting a specific object, namely a [[Part]], and naming it "Brick".  We want to [[Anchor]] it so the brick won't fall down.


Lastly, we call the function so that the function will execute.
Lastly, we call the function so that the function will execute.
Line 56: Line 56:
* [[Anchor]] that newly inserted brick.
* [[Anchor]] that newly inserted brick.
* Click that brick.
* Click that brick.
* Insert > Object > [[ClickDetector]].  This will allow the brick to detect when it is being 'clicked'.
* Insert > Object > [[GBX.lua.ClickDetector (Object)|ClickDetector]].  This will allow the brick to detect when it is being 'clicked'.
* Insert > Object > Script.
* Insert > Object > Script.
* Double-click that script file, and insert the following:
* Double-click that script file, and insert the following:

Latest revision as of 01:17, 22 September 2021

Introduction

Functions perform specific tasks. Some functions are already predefined, and some functions you create yourself. The idea behind the functions is the same.

Simple functions

Let's take a look at a simple function.

local function myfunction() -- what this function does
     local x = 2+2
     print(x)
end

myfunction() -- call the function 

Everything between function and end simply describes what your function will do. Here, you are

  • Allocating a value of 2+2 to x
  • Printing x

It's important to note that your function won't run, i.e., you won't see the value of "4" appear on your screen unless you specifically call it. That is what the last line is, namely:

myfunction()

If you delete this line, nothing will get printed to your screen.

Creating new bricks

You can make a function do all kinds of things. For example, the following function will create a new brick:

local function newbrick()
     local p = Instance.new("Part")
     
     p.Parent = workspace
     p.Name = "Brick"
     p.Anchored = true
end

newbrick()

Here, the function (called "newbrick()") is creating a new Instance of a brick. That brick will be in the Workspace. We are inserting a specific object, namely a Part, and naming it "Brick". We want to Anchor it so the brick won't fall down.

Lastly, we call the function so that the function will execute.

Action-triggered functions

Functions don't have to be called by writing out a command. They can be called by a human action, such as clicking a mouse button.

  • Insert > Object > Part
  • Anchor that newly inserted brick.
  • Click that brick.
  • Insert > Object > ClickDetector. This will allow the brick to detect when it is being 'clicked'.
  • Insert > Object > Script.
  • Double-click that script file, and insert the following:
local function clickabrick()
     print("Hi mom!")
end
  
script.Parent.ClickDetector.MouseClick:connect(clickabrick)

This function, called "clickabrick()", has to be told what to do. The function will print "Hi mom!", but only if the function is called, namely, by clicking that specific brick.

The last line is more complex now, but it has the same purpose as before -- calling the function. On the left of the colon (:), you are describing the relation of where the triggering action (i.e., a MouseClick) is to the function:

(Object).(Action):connect((Function Name))

See Also