[Tutorial] Basics of Scripting : Variables, Part customization, Printing

This is a simple scripting tutorial for beginners, to understand the very basics of scripting.

ROBLOX uses Rbx.Lua as their coding language, it’s universal throughout all of ROBLOX.

For you to make your own scripts, make sure to have ROBLOX Studio installed.

Open ROBLOX Studio and choose a baseplate. What you need opened is :
‘‘Explorer’’, ‘‘Properties’’, ‘‘Output’’, which you can find in the ‘‘View’’ tab.

Begin by pressing CTRL+I to open the Advanced Objects menu.

Select ‘‘Part’’ and ‘‘Script’’ it’ll automatically insert them in the Workspace.
image
image

Open the script, and it’ll say : print(’‘Hello world!’’) as described above, this is the coding language - Lua.

You can test your scripts by pressing F5.
As you can see, in the output, It’ll print Hello World!

However, in the actual game, the players won’t be able to see the output. Though, output is useful for Bug Testing and Checking Errors.

All changes done in the ‘‘Testing Mode’’ won’t save in the actual game.

Press Shift+F5 to exit the ‘‘Testing Mode’’.

If we’d want to print ‘‘one’’ instead of ‘‘Hello World!’’ you’d have to formulate in the correct syntax:

Incorrect Way : Print(’‘one’’) since the word print has to be used in lowercase.
Incorrect Way : print(one) since the quotation marks have to be used.

Correct Way : print(’‘one’’)

The quotation marks tell the Computer, that it’s a string ( simply text )

If you were to print three strings :

print(’‘one’’)
print(’‘two’’)
print(’‘three’’)

You’d see that they print in order.

Strings aren’t the only things that can be printed either.

You can also print

print(13.5) --float

print(true) --boolean

float Is any number.

boolean Is just true or false.

The text after the two -'swould show up as green in studio, and the green color represents ‘‘comment’’ which does not affect the script. You can make a comment by typing two -'s and everything after that would be a comment.

Let’s say you want to print Hello World! instead of Hello World five times, you’d have to change each and every one of Hello World to Hello World!

However, this gets complicated once you want to print it 100 times, that’s a lot harder.

This can be prevented by storing your variables, and done like this :
local message = '‘Hello’'

The local part defines it as a local variable, rather than a normal variable.
The difference is, it makes it slightly faster for the code to reach it, but not fast enough for you to notice it.

The message part is the name of the variable, it’s what you would use when referring back to the variable.

print(message)

It doesn’t have to be a message either, it can be anything, like
print(ThreePointO)
print(Was)

Notice, how I used letters instead of numbers, that’s because if you try to use spaces or numbers, the script gets confused. This is why it’s best practice to use letters and underscores ONLY.

Also avoid using the same variable, as that can also confuse the script :

local VariableName = '‘Goodbye’'
local VariableName = '‘Hello’'
print(VariableName)

Now with the basic syntax out of the way, we can move to the scripts, that actually affect the game.

Let’s say we want to make changes to the Part we added earlier.

Start off by storing the part to a local variable for ease of access.

local part = Part

I named the variable ‘‘Part’’.

When finding anything in the game, you need to go down the tree.

At the top of the tree ALWAYS is game, it holds everything you see in the Explorer.

game.Part

Though we can’t just do game.Part, because we need to tell the script EXACTLY where the Part is.

So we need to use

game.Workspace.Part

You can see each Part’s properties in the ‘‘Properties’’ window we opened earlier.
You can change the transparency, color, etc.

Though, we can make the scripts change it for US, like :

part.Transparency = .5

To change how transparent the part is.

0 being a brick with no way to see through it, 0.5 being glass, 1 being invisible.

Here is another example :

part.Reflectance = 1

You might think, why use scripts to change properties of Parts, if you can just open the ‘‘Properties’’ menu and select it there?

Scripting is just manipulating properties to make things do things.

These are only the basics of scripting.

9 Likes

This topic was automatically closed after 9 minutes. New replies are no longer allowed.