PcoWSkbVqDnWTu_dm2ix
Introduction to Coding
Part 2 - Functions
Introduction to Coding
Part 2 - Functions

Disappear Function

Time to make the platform disappear. It’s always best to group code for achieving a specific action into a function. A function is a named block of code that helps you organize your code and use it in multiple places without writing it again.

  • Create a function in the script and call it disappear.
local platform = script.Parent

local function disappear()
	
end

The first new line declares the function — it indicates the start of the function and gives it the name disappear. The code for a function goes between the first line and end.

The parentheses are for including additional information as needed. You’ll learn more about passing information to functions in a later course.

Part Properties

When the platform disappears, it needs to be invisible and players need to fall through it — but you can’t just destroy the platform since it needs to reappear later.

Parts have various properties that can be used here. Remember that you can see a part’s properties if you select it and look at the Properties window.

A part can be made invisible by changing the Transparency property. Transparency can be a value between 0 and 1, where 1 is fully transparent and therefore invisible.

Changing the Transparency property of the cube

The CanCollide property determines if other parts (and players) can pass right through the part. If you set it to false, players will fall through the platform.

Changing the CanCollide property of the cube

Just like script.Parent, properties are accessed using a dot. Values are assigned using an equals sign.

  1. In the disappear function, set the CanCollide property of the platform to false.

  2. On the line following, set the Transparency property to 1.

    local platform = script.Parent
    
    local function disappear()
        platform.CanCollide = false
        platform.Transparency = 1
    end
Indentation

You’ll notice that Studio automatically indents your code inside a function. Always make sure that your code is indented like this — it makes your code more readable and helps indicate where the function begins and ends.


Calling the Function

Time to run the code in the function to test that it works. Once you’ve declared a function, you can run it by writing its name with parentheses next to it. For example, disappear() will run the disappear function. This is known as calling a function.

  1. Call the disappear function at the end of the script.

    local platform = script.Parent
    
    local function disappear()
        platform.CanCollide = false
        platform.Transparency = 1
    end
    
    disappear()
  2. Test the code by pressing the Play button. If your code works, the platform should have disappeared by the time the player spawns into the game.

Appear Function

You can easily make the platform reappear by writing a function which does the exact opposite of the disappear function.

  1. Delete the disappear() line from the script.

  2. Declare a new function called appear.

  3. In the function body, set the CanCollide property to true and the Transparency property to 0.

    local platform = script.Parent
    
    local function disappear()
        platform.CanCollide = false
        platform.Transparency = 1
    end
    
    local function appear()
        platform.CanCollide = true
        platform.Transparency = 0
    end

Previous Page Getting Started Next Page Loops