Need help decoding .rbxm file data for my implementation

Hey there,

I’m currently working on a system that requires decoding the data of a .rbxm file. Here’s how my implementation looks:

-- Usage example:
local rbxmUrl = "https://quada.sub2nex.repl.co/QuadaStore.rbxm"
local importedInstance = ImportRBXM(rbxmUrl)

if importedInstance then
	-- Do something with the imported instance
	importedInstance.Parent = workspace
	importedInstance.Name = "test"
else
	-- Handle import error
	print("Failed to import RBXM file.")
end

However, I’m encountering an issue: ServerScriptService.Script:7: Failed to decode RBXM file data. - Server - Script:7

I attempted to use HttpService.JSONDecode to decode the data, but since it’s not a JSON-encoded file, it didn’t work as expected. Here’s the part where I attempted to decode it:

	local success, buildData = pcall(HttpService.JSONDecode, HttpService, xml)
	if not success then
		error("Failed to decode RBXM file data.")
		return
	end

I left JSONDecode there so you know where the decoding part is located.

I would greatly appreciate any assistance or guidance on how to properly decode the .rbxm file data. Thank you in advance for your help!

What exactly is the ImportRBXM function?
Can you please provide the documentation for it / link to its repository? Without additional information, we are unable to provide assistance

1 Like
local function ImportRBXM(url)
	local HttpService = game:GetService("HttpService")
	local xml = HttpService:GetAsync(url)

	local success, buildData = pcall(HttpService.JSONDecode, HttpService, xml)
	if not success then
		error("Failed to decode RBXM file data.")
		return
	end

	local function BuildInstance(instanceData)
		local instance = Instance.new(instanceData.ClassName)
		for property, value in pairs(instanceData.Properties) do
			instance[property] = value
		end

		if instanceData.Children then
			for _, childData in ipairs(instanceData.Children) do
				local childInstance = BuildInstance(childData)
				childInstance.Parent = instance
			end
		end

		return instance
	end

	local rootInstance
	for _, instanceData in ipairs(buildData) do
		if not rootInstance then
			rootInstance = BuildInstance(instanceData)
		else
			error("Multiple root instances found in the RBXM file.")
			return
		end
	end

	local scriptInstance = rootInstance:FindFirstChildWhichIsA("LuaSourceContainer")
	if scriptInstance and scriptInstance:IsA("LuaSourceContainer") then
		local scriptCode = scriptInstance.Source
		if scriptCode and type(scriptCode) == "string" then
			local scriptFunction, loadError = loadstring(scriptCode)
			if scriptFunction then
				scriptFunction()
			else
				error("Failed to load Lua script from the RBXM file: " .. tostring(loadError))
				return
			end
		else
			error("No Lua script found inside the RBXM file.")
			return
		end
	else
		error("No LuaSourceContainer instance found inside the RBXM file.")
		return
	end

	return rootInstance
end

-- Usage example:
local rbxmUrl = "https://quada.sub2nex.repl.co/QuadaStore.rbxm"
local importedInstance = ImportRBXM(rbxmUrl)

if importedInstance then
	-- Do something with the imported instance
	importedInstance.Parent = workspace
	importedInstance.Name = "test"
else
	-- Handle import error
	print("Failed to import RBXM file.")
end

Working on trying to get a fix for it. The current issue is that the thing given to the JSONDecode thing is not valid json.

The sort of text it tries to load is

Attempting to bypass the json decode thing just results in errors further on that aren’t resolvable.
Because of this, I am unable to provide further assistance, and I’m skeptical that anyone would be able to.

Is there a particular reason you need to access and load an rbxm file from a URL? There may be a more appropriate method of achieving what you need.

I am attempting to create a package manager where you upload a .rbxm file to a site, then you can do like local blahpackage = somemodule.require("blahpackage") and it will insert it to the game.

Is it possible you can download it as an RBXMX file?

Is it possible to import that to ROBLOX as it was created, like if I save a lua script to that format, it will import it as a lua script with the same source inside it again.

I’m not sure. All models I download appear to be in the RBXM format. You could try it though.

Try that library, it parses the RBXM into a virtual tree that you can then script on. It doesn’t build the model because Roblox is really funky with some of the older properties

why are you trying to jsondecode a roblox model file? you know it has nothing to do with json format

but since it’s not a JSON-encoded file

I knew it, but I wanted to see what result I gained.

Can you give an example of how we use this to decode it?

local lxm = require(path.to.lxm)

local RBXM = lxm(RBXMFileStream)
print(RBXM.Tree) --> the model tree

I’m confused as I am new to doing this advanced methods, but how would I do it by a URL.

local HttpService = game:GetService("HttpService")
local lxm = require(path.to.lxm)

local success, response = pcall(HttpService.RequestAsync, HttpService, {
  Url = "https://quada.sub2nex.repl.co/QuadaStore.rbxm"
  Method = "GET"
}) -- yes you can use methods like this, pretty cool

if success then
  local rbxm = lxm(response.Body)
end

Yeah alright. But if we wanted to later on convert the decoded file into a Lua file if you know how to.

There are two things I can think you want

1. Store the RBXM in a script

Just use the Body of the HTTP request?

You cant store the binary contents into the script directly, since scripts are read as UTF-8 streams, not binary, the only way to do it would be encode it in Base64 before writing it to the script.

2. Create a new RBXM

LXM cant do this, there is no LZ4-compliant Lua compressor (technically i can just write uncompressed streams directly to the RBXM but that feels like it would add a load of unneccessary data)

so why did you even post this if you knew it wouldnt work?

I have a possible method and therefore this case is closed.

1 Like