3.9 KiB
| title | description |
|---|---|
| Script types and locations | How scripts run in Roblox, and how location impacts that behavior. |
import ScriptLocations from '../includes/engine-comparisons/script-locations.md'
For many developers, the fundamental challenge of adapting to Roblox scripting is the importance of file location and the Class.Script.RunContext property. Depending on script type, location in the Explorer, and run context, scripts can behave very differently. Certain method calls might fail, objects in your experience might be inaccessible, or scripts might not run at all.
The reason for this complexity is that Roblox experiences are multiplayer by default. Scripts need the ability to only run on the server, only run on the client, or be shared across both. The evolution of the Roblox platform over time has further complicated the situation.
Script types
Roblox has three types of scripts:
Class.Script- Code that runs on either the server or the client, depending on its location andClass.Script.RunContextproperty.Class.LocalScript- Code that runs only on the client. Does not have a run context.Class.ModuleScript- Code that you can reuse in other scripts. Does not have a run context.
When you create a Class.Script, its default run context is Legacy, meaning that it a) is a server-side script and b) only runs if it is in a server container, such as Class.Workspace or Class.ServerScriptService.
- If you change the script's run context to
Server, it can now also run inClass.ReplicatedStorage, but that's not recommended. The contents of that location are replicated to clients, so it's a poor location for server-side scripts. - If you change the script's run context to
Client, it can run inReplicatedStorage. It can also run inClass.StarterCharacterScriptsandClass.StarterPlayerScripts. Starter containers are copied to clients, though, so the original script and the copy run, which isn't desirable.
To change a script run context, select it in the Explorer and change the value in the Properties window.
Recommendations
-
Using
ServerandClientvalues forRunContextremoves ambiguity from how scripts run and can help keep your project organized. The best use cases for non-defaultRunContextvalues are: -
To share code between server and client scripts, use
Class.ModuleScript|ModuleScriptsinReplicatedStorage. -
Use
Class.LocalScript|LocalScriptsinStarterCharacterScripts,StarterPlayerScripts,StarterGui, andStarterPack.
Script locations
This image shows which Explorer window locations can contain client scripts. Remember, ReplicatedFirst and ReplicatedStorage can contain Class.Script|Scripts with a Class.BaseScript.RunContext|RunContext of Enum.RunContext|Client, whereas the Starter[] containers should use Class.LocalScript|LocalScripts.
Example project structure
The Plant reference project shows how you might organize your code in a large, complex experience.
Of particular note is how it stores the vast majority of its code as reusable Class.ModuleScript|ModuleScripts in ReplicatedStorage and ServerStorage.