How do you make an auto-update module?

Clearly, I tried everything with InsertService:GetLatestAssetVersionId(***) and somehow, nothing comes out to update. I got very confused on how to make it. How can you make it? Is it possible?

5 Likes

What do you mean by “Auto-Update Module”? If you mean a module that updates when you re-upload it to the website, it’s not possible as the first time it is required will cache the initial module on that server instance.

Example:

local Module = require(0000000) --initial require gets cached
Module:DoSomething()

Re-uploads new module

Module = require(0000000) --attempts to require module again
Module:DoNewThing() --throws error because the old module is cached and does not contain the new method

basically like how admin essentials have a automatic update to get them updated.

As long as you publish it to the main module, updates are global for the same asset Id.

EDIT: All of those Admin sources are open sourced now due to Roblox removing closed source support so feel free to take a look.

1 Like

As others already said, the way admin scripts “auto update” is simply updating the admin’s MainModule. That way when they are required in new servers, the new version is loaded.

How to make a module like that:

  1. Create a new ModuleScript
  2. Name it MainModule
  3. Put your code into it (and parent your assets to it if you use any)
  4. Right click it > Save to roblox > Create a new model or override an existing one
  5. Copy the id from the url
  6. Now you can use require(id) in a script to load the module! (Doesn’t work in studio, unless they changed it, but I don’t think so)

From that point you can update the module and have it update in every place it’s used without the need to change the game’s code.

Note that if you want the module to be able to be loaded into a game not owned by you, you have to make it uncopylocked.

1 Like

Hm, that works, however, I uncopy locked and it won’t work. If I use, InsertService:GetLatestAssetVersionId(), is it possible?

1 Like

If you want to create something closed source then you’ll need to use a remote server to download the script from and loadstring it

But I’ll let you figure out security for yourself

This is actually possible, however this will only work on a live server (Within a place published to the site).

-- Services
local InsertService = game:GetService("InsertService")

-- Modules
local Module

-- Constants
local MODULE_UPDATE_TIME = 60
local MODULE_ID = 3208522146

-- Variables
local moduleLastId
local moduleLoaded = false


function getLatestModule()
	local latest = InsertService:GetLatestAssetVersionAsync(MODULE_ID)
	if moduleLastId ~= latest then
		moduleLoaded = false
		moduleLastId = latest
		local module = InsertService:LoadAssetVersion(moduleLastId):GetChildren()[1]
		Module = require(module)
		moduleLoaded = true
		print("Module has been updated")
	else
		print("Module is already up to date")
	end
end

do
	spawn(function()
		while true do
			getLatestModule()
			wait(MODULE_UPDATE_TIME)
		end
	end)
end

Where the variable “Module” is the actual module that you want to use.

With this method, you don’t need to name the uploaded module (model) “MainModule”, and can instead name it whatever you like, provided you / your game has ownership of that model.

14 Likes

What if you tried setting Module to nil (removing all references to it as well) and doing a garbage collection? I haven’t tried this myself as I’m busy doing something else ATM but I don’t see why the garbage collection wouldn’t work…

require follows memoisation patterns. When you require the module, it is permanently cached in memory for the game session and any subsequent requires will return that cached value. Erasing all traces of the module from your scripts will not clean it out from memory and you cannot force garbage collection cycles either.

2 Likes