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
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
The parentheses are for including additional information as needed. You’ll learn more about passing information to functions in a later course.
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.
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.
script.Parent, properties are accessed using a dot. Values are assigned using an equals sign.
disappearfunction, set the CanCollide property of the platform to false.
On the line following, set the Transparency property to 1.
local platform = script.Parent local function disappear() platform.CanCollide = false platform.Transparency = 1 end
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.
disappearfunction at the end of the script.
local platform = script.Parent local function disappear() platform.CanCollide = false platform.Transparency = 1 end disappear()
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.
You can easily make the platform reappear by writing a function which does the exact opposite of the
disappear()line from the script.
Declare a new function called
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