Script Types and Locations

*เนื้อหานี้จะพร้อมใช้งานในภาษาที่คุณเลือกในเร็วๆ นี้

For many developers, the fundamental challenge of adapting to Roblox scripting is the importance of file location and the 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:

  • 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.

When you create a 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 Workspace or ServerScriptService.

  • If you change the script's run context to Server, it can now also run in 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 in ReplicatedStorage, but it can also run in StarterCharacterScripts and StarterPlayerScripts. These 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.

RunContext property indicated for a Script.

Recommendations

  • Use the default RunContext value unless you have a specific reason not to. The Legacy name is meant to convey that Roblox scripts have traditionally worked this way and is not an indicator that the behavior is deprecated or suboptimal.

  • There are two good use cases for non-default RunContext values:

    • Client scripts that you want to run from ReplicatedStorage or ReplicatedFirst.
    • Server or client scripts that you include within models and packages. Explicitly setting the run context makes models and packages more likely to work properly from a variety of locations.
  • To share code between server and client scripts, use ModuleScripts in ReplicatedStorage.

  • Use LocalScripts in StarterCharacterScripts, StarterPlayerScripts, StarterGui, and StarterPack.

Script Locations

LocationDescription
WorkspaceRepresents the game world. Only runs server scripts. This location works well for scripts that attach directly to objects and control their behavior.
ReplicatedFirstContains objects that replicate to the client before anything else. This location is ideal for the absolute minimum set of objects and scripts necessary to display a loading screen.
ReplicatedStorageContains objects that are replicated to both the client and the server. This location is ideal for ModuleScripts that you want to use on both the server and the client. LocalScripts do not run from this location, but Scripts with a run context of Client do.
ServerScriptServiceContains server-side scripts. This location is ideal for scripts that need to access server-side functionality or objects, such as game logic and cloud storage.
ServerStorageContains server-side objects. This location is ideal for large objects that don't need to be immediately replicated to clients when they join an experience. Scripts do not run from this location, but you can store server-side ModuleScripts here.
StarterPlayer.StarterCharacterScriptsContains LocalScripts that run when the character first spawns. These scripts do not run again when the player respawns.
StarterPlayer.StarterPlayerScriptsContains general-purpose LocalScripts that run when the player joins the experience.
StarterGuiContains GUI elements that the client displays when it loads the game. LocalScripts can run from this location. This location is ideal for scripts that modify the game's user interface, such as adding buttons, menus, and pop-ups.
StarterPackGenerally only contains Tools, but can also include LocalScripts for setting up player backpacks.

This image shows which Explorer window locations can contain client scripts. Remember, ReplicatedFirst and ReplicatedStorage can contain Scripts with a RunContext of Client, whereas the Starter[] containers should use LocalScripts.

Diagram showing which script locations run on clients.

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 ModuleScripts in ReplicatedStorage and ServerStorage.