Linked scripts are a feature that allows one piece of source code to be reused across multiple
Script objects. Each type of Roblox script (
ModuleScript) has a property called
LuaSourceContainer/LinkedSource. Historically, this was to allow Roblox engineers to use premade scripts written by Roblox, but this property can now also link to user-made scripts.
When developing a game it is useful to reuse code. Many scripted elements in game, such as NPCs or weapons, need to appear multiple times in a level. While copying and pasting each game element is a quick way to do this, several problems can arise. If one of the scripts has an error, then each script will also have that error. Fixing one script means each script will have to be individually edited, or all of the copied elements have to be deleted and copied from the fixed one.
Creating a linked source
A linked source is the asset that stores the source code that other scripts will be able to link to.
To create a linked source the game where you want it to be used must be published. Linked sources are assets, like images, and can be shared across all of the places in a game. This means that the game must be published on Roblox.
After publishing, select a script in the Explorer. This can be any type of Roblox script such as a Script, LocalScript, or ModuleScript. Right click on the script in the Explorer and select “Create new LinkedSource”. You will be prompted to give your source a name. This name has to be unique from any other LinkedSource currently in the game. Once you have typed in a name, click “Create” to finish making the LinkedSource. After this, the Game Explorer will show the new LinkedSource under the Scripts folder in its Assets. Also, the script that was selected to create the source will automatically point its LinkedSource property to this newly created source.
Using a linked source
After a LinkedSource has been created, it can be linked to by any script in its game. To do this, select the script in the Explorer and click on the LinkedSource property. This will pull up a dropdown of all of the LinkedSources in the game. Selecting any of these sources will link the script. Any type of script can link to a LinkedSource. Different types of scripts can even link to the same LinkedSource, although normal script limitations will still apply. For example, if a LinkedSource has code that accesses a Data Store, this code will not work if a LocalScript links to that LinkedSource.
You can also right click the LinkedSource in the Game Explorer, which will give you the options to insert the LinkedSource as a Script, a LocalScript or a ModuleScript. When clicked, the selected script type will be inserted in the game with its LinkedSource property set to the selected source. The new script will be parented to whatever you currently have selected in the hierarchy Explorer. If nothing is selected, or multiple instances are in the selection, then the new script will be parented to the workspace.
After a script has been linked, any code that was originally in the script will no longer be evaluated. Instead, when the game runs the script will run the code that it pulls from its LinkedSource. You can remove a script’s link by selecting the LinkedSource property again and selecting “[Embedded]”. This will unlink the script and the script’s original code will be evaluated when the game runs.
Editing a linked source
The chief advantage of using a LinkedSource is that if you make changes to the source, the scripts that link to it will automatically use the new code defined in the source.
You can edit a LinkedSource in two ways. Firstly, you can double click on the LinkedSource in the Game Explorer to open an editor to make changes. Alternatively, you can also double click on any script that is linked to a LinkedSource. Doing so will open the LinkedSource in the script editor so you can make changes.
If you make changes to a LinkedSource you can test them right away in Studio. If you are satisfied with the changes, they will be saved the next time you publish the game to Roblox. If you do not like the changes, you can right click on the LinkedSource in the Game Explorer and click “Revert to Last Published Version”. This will revert any changes you have made to the LinkedSource since the last time you published it.
ModuleScript— an alternative to using linked scripts is to put code in a module, and have the script in the weapon or NPC contain just a call to that module