I’ve written a simple Module to allow for the live updating of ModuleScripts, bypassing the caching behavior of require(AssetId)
and InsertService:LoadAsset(AssetId)
. You can get the Module here: https://www.roblox.com/library/5146832535/ModuleScript-Live-Updater
This is useful for keeping Modules up to date in live servers without the need for shutting down the game. Personally, I use it to keep things like promo codes, shop prices, and weapon statistics fully dynamic.
The usage is simple - here’s an example of how it can be used to keep a list of promo codes updated within the scope of this script:
local ModuleLoader = require(5146832535)
local PromoCodesAssetId = 4915506641 --AssetId of your PromoCodes ModuleScript
local PromoCodes = require(PromoCodesAssetId)
while true do
local UpdatedModule, VersionAssetId = ModuleLoader.GetLatest(PromoCodesAssetId)
if UpdatedModule ~= nil then --Ensure that the ModuleScript has properly loaded
PromoCodes = UpdatedModule
end
wait(15)
end
And voila, now your table of PromoCodes
will stay updated in the above script! Note that the ModuleLoader will require()
the updated ModuleScript, so its code will be executed each time it’s loaded. The code is pretty easy to modify, should you wish to change this behavior.
How does it work?
It’s pretty simple. Basically, every time ModuleLoader.GetLatest(AssetId)
is called, the VersionAssetId of the ModuleScript model is loaded and compared to the last VersionAssetId saved for that Module. If the VID changes, then the Module is re-inserted using InsertService:LoadAssetVersion(VID)
.
This bypasses the caching you’d expect on subsequent calls of require(AssetId)
or InsertService:LoadAsset(AssetId)
, allowing the Module to be updated in a live game server.
How do I update my Module?
Create a ModuleScript and name it MainModule. Naming it MainModule isn’t necessary unless you’re planning on calling require()
with the AssetId as the parameter. Then, right click on the ModuleScript in the Explorer window of studio, and select the “Save to Roblox” option. The AssetId of that model will be the one you use. Whenever you want to live update your Module, simply make your changes to that ModuleScript, and then click “Save to Roblox” again.