Introduction to Scripting in Roblox - UnicMinds (2024)

What is Scripting in Roblox?

Scripts are plain text files that let you add custom, dynamic behaviour to your experiences. You can use scripts to trigger in-game events, respond to player input, save player data, create leaderboards, spawn enemies, and much, much more.

Script Types – Roblox has three types of scripts:

  • Script – Code that runs on either the server or the client, depending on its location and Script.RunContext property.
  • LocalScript – Code that runs only on the client. Does not have a run context.
  • ModuleScript – Code that you can reuse in other scripts. Does not have a run context.

Creating a Script in Roblox

Scripts are commonly created in ServerScriptService, a special folder made just for holding scripts. In Explorer, hover over ServerScriptService to see the + button.

Introduction to Scripting in Roblox - UnicMinds (1)

Click the + button and select Script. A new script will be created and the script editor will open.

Right-click Script and select Rename. Name the script PracticeScript. Naming scripts helps you and any teammates remember what each script does.

Simple print statements in Lua

New scripts include a print function at the top of the script editor. Print functions display text on the screen. It’s one of the first functions many people learn, and you’ll use it often. This code will make “Hello world!” appear on the screen.

Default code

print(“Hello world!”)

To find the script next time you open up Roblox Studio, click on the name of the script above the game editor, or double-click the script’s name in Explorer.

Testing Output

You can see the result of running the default code with the Output window. The Output window is typically at the bottom of Roblox Studio, but if you’ve never used it before, you may need to enable it.

You’ll need the Output window throughout this course, so now is a good time to enable it if you haven’t already.

Select the View menu tab.

Click Output.

Introduction to Scripting in Roblox - UnicMinds (2)

To test the script, click Play. Hello world! will show up in the Output window

Introduction to Scripting in Roblox - UnicMinds (3)

Click Stop to end the playtest. You can now return to the Script tab.

Data types and Variables

Coding languages classify different kinds of values into data types. For example, one data type is a number. Number data types are self-explanatory as they are made up of only numbers.

Another data type is a string. Strings can hold numbers, letters, and characters. Take another look at the default code in the new script; the words and quotations within the parenthesis is an example of a string data type.

Strings like “Hello World” always sit inside quotation marks, “like this”. More examples of strings are below. Notice how they hold a mix of letters and numbers.

“You joined the game!”

“Hello123$”

“10”

In Lua, to declare a new variable, type local, then type the name for the new variable.

In Lua, variables can be global or local. You’ll usually use local variables. Local variables can only be used within the script or chunk of code where they were created. Global variables can potentially be used by other scripts.

Declares a new variable

local myAvatar

Assigning Values to Variables

New variables are empty. To assign it a value, or put something inside its container, use the = symbol. In this case, assign the variable the name of your favourite player.

Using Print() For Your Own Messages

Print functions display text on the screen, as you saw earlier. It’s one of the first functions many people learn since it’s a simple way of giving the script a command. To see your variable, use the print() function.

On a new line, type print().

Adds empty print()

local myAvatar = “Noob”

print()

Type the name of your variable within the parenthesis.

Outputs “Noob”

local myAvatar = “Noob”

print(myAvatar)

If you see a red error in the output editor, it’s like there’s an error in the code. Each error has a unique way of describing itself. Try and use the information from the error to fix your code.

Test your code with the play button. You should see the name of your animal in the Output window.

Combining Strings

You can display any string in the Output using print(); you can even print multiple strings stored within variables or typed directly within the function. Concatenation is combining strings. To concatenate the string assigned to your variable and a second string, use two dots .. The following example concatenates two variables and two typed strings.

Uses variables and strings together

local firstAvatar = “Noob”

local secondAvatar = “Denis”

print(“I like ” …firstAvatar .. ” and ” .. secondAvatar)

Output: I like Noob and Denis

Introduction to Conditionals in Lua

Scripts use conditional statements to handle these types of situations. Conditional statements are lines of code that only run if certain conditions are true. One type of conditional statement is an if/then statement. In Lua, the syntax pattern for if statements looks like this:

if then syntax

if “something happens” then

— Make something else happen

end

if then

— empty code

end

if 3 + 3 == 6 then

— empty code

end

Computer checks if the condition “3+3==6” is true , then the code written inside the if block will be read and executed by the script.

For Example:

local myAvatar=”Noob”

If myAvatar==”Noob” then

print(“Hello”)

else

print(“Bye”)

The script checks if the value of myAvatar is Noob, if it’s Noob, the output will be Hello.

local myAvatar=”Noob”

If myAvatar==”Denis” then

print(“Hello”)

else

print(“Bye”)

Here , the output will be Bye as the condition is false.

Multiple Conditions

Control structures can have more than one condition. The keywords else and elseif can create additional scenarios for what should happen under several conditions. The syntax looks like:

local myNumber=100

Shows syntax for elseif and else

If myNumber>0 then

print(“Go”)

elseif myNumber<0 then

print(“Slow”)

elseif myNumber==0 then

print(“Wait”)

else

print(“stop”)

end

The computer will check the value of myNumber in the first condition, as it’s true, the output will be Go. As the Script got it’s output, the script will not read the next set of code.

local myNumber=0

Shows syntax for elseif and else

If myNumber>0 then

print(“Go”)

elseif myNumber<0 then

print(“Slow”)

elseif myNumber==0 then

print(“Wait”)

else

print(“stop”)

end

The computer will check the value of myNumber in the first condition, as it’s false, the script will go to the next condition , which is also false. Then it will go to the next condition , which is true.

Output will be stop.

Using AND / OR operator in multiple Conditions

if finishTime < 5 then

— Get a gold medal

end

if finishTime >= 5 and <= 10 then

— Get a silver medal

end

if finishTime > 10 and <= 15 then

— Get a bronze medal

end

Using and operator in if statements enables the script to check both the conditions finishTime >= 5 and <= 10. If both the conditions are true, then the script runs the code.

Hope this is useful, thank you.

You may like to read: Common Coding Terms for Kids, Making Scrolling Background in Scratch, & Nurturing Curiosity in Children

Introduction to Scripting in Roblox - UnicMinds (2024)

References

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6139

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.