RoGo Magazine - Beginner Lua Lesson 8: Difference between Server and Local Scripts

.

Posted in Tutorials

August 5, 2015 at 11:42 AM

Beginner Lua Lesson 8: Difference between Server and Local Scripts

By DerAtomik

Editors: Wegggieninja and Mirthmaker

Previous Beginner Lua Lesson

First Beginner Lua Lesson

Welcome back! It’s been awhile since I wrote a Beginner Lua Lesson! Nevertheless, it’s time to learn about the differences between server and local scripts!

What is a local script?

A local script (class name of LocalScript), is a script that will only run when it is in a player’s PlayerGui folder (where ScreenGUI’s go), in a Tool/HopperBin that is in a player’s Backpack, or when the Local Script is in a player’s Character Model.

What is a server script?

A server script will run just about anywhere. Most preferably, you’d want to place them in the ServerScriptService. The only places a server script will NOT execute its code is where a local script would when Filtering is enabled (click here to see a tutorial by lion2323 on filtering), and when it is located in the Lighting, ServerStorage, ReplicatedFirst, or ReplicatedStorage.

Do the differences matter?

Yes, not only do these scripts execute in different places, but the two have certain functions that can only be done by either one. Let’s talk about those.

Code only executed by local scripts

An advantage a local script has over a server script is, since it only runs when it’s inside a player, it can easily detect that player. You can find what player the script is executing inside by using game.Players.LocalPlayer. For example:

local player = game.Players.LocalPlayer
print("I'm being executed by "..player.Name.."!")

But that’s not all. Some Services will only execute in a local script, and vice versa. They are the following: ContentProvider (loads assets in advance), and the player’s CurrentCamera (accessed through workspace.CurrentCamera).

Code only executed by server scripts

There are a lot more things only accessible by server scripts. These include ServerStorage, and the following Services: DataStoreService (click here for a tutorial on Data Stores), and PointsService (Player Points), among others. Whenever you learn about a new Service, be sure to check if it’s only accessible by a certain type of script!

Conclusion

So now you know that both kinds of scripts have their own purposes! There is a third type of script, ModuleScript, but those are complicated. Search them on the ROBLOX Wiki to read more about them.

As always, if you’ve got a question or you need help, send me a PM or leave a comment! I’ll see you in the next Beginner Lua Lesson! :)