Basic Scripting - 1

Learn about functions, variables, and instancing!

by Crazee_Monkee

Author Avatar

Introduction

Welcome! I'm Crazee_Monkee! I'm still learning scripting, so there are some things I don't know yet.

First off, what are functions, variables, and instancing?

A function is a defined set of code that can be triggered when called upon by name. Using them makes scripting faster and easier!

A variable stores data on a part, string, etc. also making scripting faster and easier!

Instancing allows you to insert something, like a part, into your game via script!

Variables

Let's try making a variable.

Let's say you had a part, but it has a long name. Maybe it's called birchtree, but to make scripting quicker, let's call it birch using a variable. (Variables will be much more important when using instancing and functions, but to make it simple I'll use this example.)

You must include local before the name of your variable.

local birch = game.Workspace.birchtree -- You must include all parents of your part. Parents are all the objects your part/model is inside of.

Great! You've created a variable. The reason we have to put game.Workspace in front of birchtree is that they are the parents of your part.

Now let's try making a variable for a string. String is just text. Using a variable can make it so you can put the same text over and over again quickly, so you dont have to keep typing it out. Make sure you have output open (it's under the view tab) so you can see your text! I'm going to call my string 'text'.

local text = "'Meow!' went the cat."

Your string must be in quotes! ("") Now, if you run your game, you wont see the text. This is because we have to use the print() function (we'll learn about built-in functions in a later tutorial).

local text = "'Meow!' went the cat."
print(text)

Normally, we would have to type out the string, but not anymore! And, you can duplicate it as many times as you like. Try pressing 'Run' and you'll see the text in Output.

There! Now you know how to make a variable for shortcuts! :)

Functions & Instancing

Let's try making a function using instancing! We're going to insert 3 parts in our game using a script.

First, give your function a name.

function addPart() -- the parenthesis are your function parameters. This is so you can send data to different parts, and make one part red, and another green. We don't need to use this yet, but your function won't run without parameters.

The function won't do anything if you call it by name, since we haven't coded anything. Let's make it add in a slightly transparent green part that is anchored.

function addPart()
    local part = Instance.new("Part") -- instancing must always be a variable or it will not generate the part.
    part.Name = part -- you can give your part any name you want!
    part.Transparency = 0.5 -- set this to 0 for it to be solid.
    part.BrickColor = BrickColor.new("Sage green") -- you can set it to any color!
    part.Size = Vector3.new(5,5,5) -- Vector3 is the format of the size/postion (x,y,z)
    part.Position = Vector3.new(5,5,5)
    part.Parent = game.Workspace -- always put where you want the part (ServerScriptService, Lighting, Workspace, etc.) last. its easier to give the part all its properties THEN place it.
end

Finally, we have to call the function by name to make it run. Always put the trigger (when you call it by name AFTER. You can call it by name multiple times to add in more parts!

function addPart()
    local part = Instance.new("Part") -- instancing must always be a variable or it will not generate the part.
    part.Name = part -- you can give your part any name you want!
    part.Transparency = 0.5 -- set this to 0 for it to be solid.
    part.BrickColor = BrickColor.new("Sage green") -- you can set it to any color!
    part.Size = Vector3.new(5,5,5) -- Vector3 is the format of the size/postion (x,y,z)
    part.Position = Vector3.new(5,5,5)
    part.Parent = game.Workspace -- always put where you want the part (ServerScriptService, Lighting, Workspace, etc.) last. its easier to give the part all its properties THEN place it.
end

addPart()
addPart()
addPart()

And now you're done! If you press 'Run' 3 parts will appear in your game. When you press 'Stop' it will disappear.

---

If you have any questions, leave them in the reviews and I'll edit this as needed!

View in-game to comment, award, and more!