All the types of scripts!

Learn all three types of scripts that make a roblox game!

by setmetatab1e

Author Avatar

Hello, in this tutorial you will learn what LocalScripts, Scripts and ModuleScripts are. This is going to be a fairly simple tutorial and requires you to have very little knowledge on scripting. I'll provide code examples, usages, and when you should use them!

For using some of these examples, they have to be the RIGHT script, RIGHT location in the explorer and the RIGHT spelling in order to work.

If your script is not working then you should probably look in the Output window and see if the script gave an error. If not then the script is Disabled, if not then you just placed it in the wrong location in the explorer.

LocalScript

The first script, designed for player interactive scripting which can be GUIs, player input, remotes, current camera, and more client based systems. You should use LocalScripts when you know something that needs to be controlled by a player (e.g a gun, spectate systems) Those are all controlled by the player but the LocalScript is listening to what the player does and then executes whatever.

The LocalScript should be placed under a player in the Explorer Menu. This means StarterGui, StarterPack, StarterPlayerScripts and StarterCharacterScripts are all viable options to put LocalScripts in, why? Well when you put those scripts in those "folders" and then a player joins, those scripts in the folder will get cloned to the player itself. That's why scripters put the LocalPlayer in the LocalScript even though it's not in the player at the start.

A LocalScript is espically useful for getting the LocalPlayer. The LocalPlayer is the Player that your script is in. If you and me both had LocalScripts, my LocalPlayer would be myself and your LocalPlayer would be you.

LocalScripts can't do everything in the game themselves. An example is say you have a wizard that wants to spawn a Brick. A LocalScript can listen whenever they want to spawn a part but whenever they do spawn a part, only that LocalScript is gonna create the Brick.

Which basically means, the wizard who spawned the brick is only going to see it and interact with it. No other player can see what the wizard did. WHich is why you need Scripts in your game too.

Here is some code example of using LocalScripts to get the LocalPlayer and change the Current Camera's CFrame to the LocalPlayer's Left Arm. This will only work with R6 rig types.

-- // LocalScript \\ --

-- localscript in StarterGui

local Players = game:GetService("Players") -- getting the Players service for the LocalPlayer

 -- getting the local player, the player that has this localscript as a descendant
local LocalPlayer = Players.LocalPlayer

-- getting the CurrentCamera to change the CFrame of the camera
local Camera = workspace.CurrentCamera

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait() -- waiting for the character to load, the character is the avatar apperance of the player.

local LeftArm = Character["Left Arm"] -- Getting the left arm, you can get children with spaces this way too.

-- Changing the Type of the Camera to Scriptable, allowing for a scripted camera.
Camera.CameraType = Enum.CameraType.Scriptable

-- Now changing the CFrame of the Camera
Camera.CFrame = LeftArm.CFrame

img|65x50

Image of a LocalScript, identified with a player "head".

Script

Scripts are basically used for everything that LocalScripts and ModuleScripts don't do. Scripts can be used for many things. Everything except client based material is applicable. Scripts only execute when it is in a descendant of the Workspace or ServerScriptService. Making it more of a server sided type of script.

Scripts can do everything basically everything except for what LocalScripts can do. This is useful for creating AI, datastores, handling remotes, leveling up systems, leaderboards and more.

However, the player's character is a descendant of the workspace. The workspace is everything you see possible in the game except for GUIs. That mean's if you put a script in StarterCharacterScripts, it's now on the client of the player. This also works with tools only when equipped, since when you equip a tool it parents it to your character. Scripts will run in a client side environment if this happens

Purposes of client sided scripts can be animations, welding scripts that stick parts together instead of anchoring, visual effects, sounds and more non-client based systems.

Here is an example of using Roblox's leaderboard system to make an leaderboard. This script is in ServerScriptService.

local Players = game.Players -- getting players service

Players.PlayerAdded:Connect(function(player) -- listening for players being added in the game
    local leaderstatsFolder = Instance.new("Folder", player) -- creating new folder for the player to keep leaderstats in
    leaderstatsFolder.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstatsFolder) -- creating a new int value for the player to keep how much money they have
    money.Name = "Money" -- the name of the stats creating
    money.Value = 50 -- changing their money to 50 dollars
end)

img|65x50

Image of a Script, identified by a bright blue background.

ModuleScript

A script that's only purpose is to share, organize and write code in a neat way. You can organize your whole game with ModuleScripts and the benefit of it is that you don't need to scroll so far in your other scripts to find something that you need to fix.

ModuleScripts don't have only that though, they can be used for Object Oriented Programming, making APIs and libaries and you can even execute part of the module script if you wanted to!

The only catch is that you need to "require" the ModuleScript with either a LocalScript or Script. Requiring basically means that you can share the code with the two scripts. You can require the ModuleScript multiple times with different scripts if you want to share code with different scripts. ModuleScripts can also require other ModuleScripts too.

ModuleScripts technically work everywhere in the Explorer Menu, but they have to be required by a LocalScript and Script, which both have their own restrictions to where they can execute.

So many people write amazing things using module scripts because it is easy for the user to execute which part and have full control of the modulescript too.

If a ModuleScript exists under a player's GUIs or anything client related the ModuleScript will run in its own environment. Meaning the ModuleScript can't access the ServerStorage if it is under a player. But a ModuleScript in the server (not in the player) can access the ServerStorage.

I won't provide a ModuleScript example since it is a bit too complicated but you can get the purpose and why you should use them.

img|50x50

Image of a ModuleScript, identified with a grey 2x2 brick.

Conclusion

LocalScripts are scripts that are used for player interactivity and listens for what the player does and other client related scripting.

Scripts are scripts that do everything except for what LocalScripts do. They can be used as client scripts only when it is under the Workspace.

ModuleScripts are scripts that can share code and execute which ever part you want. They also are used to organize and format code.

This has been my second tutorial and if you would like to learn more you can ask me in the Reviews section. Check out my Camera Manipulation Tutorial if you want to learn how to manipulate the camera!

- 123nabilben123

Update: May, 5, 2020

You guys really loved this tutorial a lot! In return I can still make some suggestions to this tutorial. Ask me in the Reviews section below.

I wanted to say this but if you had problems with your script, then you should visit this website called "Scripting Helpers". It's a community based QnA website which you can get help for your scripting problem and help others too!

read every single review daily. Please comment suggestions, critiques, or things that you guys want.

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