Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to have the same script on a bunch of different games?

Asked by 5 years ago

By this I mean, lets say you have a script that you want on different places in your game. Whenever I'd want to change the script, I'd have to change the script on all places. Is there a way to store the script somewhere where all the places would be able to access it, so I only have to change the script one? Thanks in advance!

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The standard method is to create Linked-Scripts which basicly treats the code (source) as a game asset.

To create a linked script right click on the script and select "Create new LinkedSource". To use the code in another place create a script and select a new LinkedSource.

Note that you need to publish any changes made in the source code as they do not upload when published. You need to goto the game explorer, right click then publish the changes there.

There are however other options but these are currently not upto the task as private module are being removed and packages do not currently auto update across your game.

I hope this helps. Please comment if you have any other questions about how this works.

0
Note that linked source goes back to before modules were available in Roblox. User#5423 17 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

ROBLOX has a handy feature called ModuleScripts. You can think of a module script essentially as another table in your scripts. It can store values, including functions, so you can use them in all your scripts. The main purpose of them is to reduce clutter and avoid repeated code. To utilize them, we use the built-in require() function, accompanied by either an object value referencing the module script in your game, or a rbxassetid.

Using an asset ID, you can utilize module scripts that aren't even in your game. To do this, name the module script MainModule and publish it to the ROBLOX library. You then simply run require("AssetId"). Here's a couple examples of the usage of module scripts.

Note: this code assumes you have a script in ServerScriptService with a ModuleScript inside it, as well as the defined bricks in the workspace.

--Module Script

local module = {
    Kill = function(humanoid)
        if humanoid then
            humanoid.Health = 0
        end
    end,
    Heal = function(humanoid)
        if humanoid then
            humanoid.Health = 100
        end
    end,
    Respawn = function(player)
        if player then
            player:LoadCharacter()
        end
    end
}

return module

--Server Script

local module = require(script.ModuleScript)
---
local KillBrick = workspace:WaitForChild("KillBrick")
local HealBrick = workspace:WaitForChild("HealBrick")
local RespawnBrick = workspace:WaitForChild("RespawnBrick")


KillBrick.Touched:Connect(function(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    module.Kill(humanoid)
end)

HealBrick.Touched:Connect(function(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    module.Heal(humanoid)
end)

RespawnBrick.Touched:Connect(function(part)
    local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
    module.Respawn(player)
end)

Note that you can also store normal values, such as NumberValues, IntValues, and ObjectValues inside modules. Here's a quick example.

--Server Script

local module = require(script.Module)

game:GetService("Players").PlayerAdded:Connect(function(player)
   if player.AccountAge < MinimumAccountAge then
      player:Kick("Your account is not old enough!")
   end
end)

--Module Script

local module = {
   MinimumAccountAge = 365
}

return module

I prefer to do all my checks (such as checking if the humanoid exists) in my modules. This keeps my main code nice and clean. I hope this gives you insights into the wonderful world of modules. Happy coding!

Resources:

Module Scripts


Accept and upvote if this helps!

Answer this question