I am scripting a model exporter plugin for a Roblox game called “RetroStudio”, however there is one thing that I couldn’t seem to find out. Whenever the JSONs get inputted into the Roblox Studio plugin interface, the “Create” function in the importer module does not seem to return a list of properties and instead only returns one property and its value.
The importer module is scripted as follows:
local httpService = game:GetService("HttpService")
local constructors = require(script.Constructors)
local plugin = script.Parent
local cleaned = 0
local baseParent = nil
local definedParents = {}
local function construct(n, v, obj)
local func = constructors[typeof(obj[n])]
if func then
return func(v)
else
return v
end
end
local function refine(jsons)
-- Refines every JSON submitted to the importer UI, returning a proper Lua table
-- that the model importer can fully use without doing multiple
-- operations in the background.
local function refineError(reason)
return {false, reason, {}}
end
if jsons == nil then return unpack(refineError("No JSONs were input.")) end
local success, error = pcall(function()
jsons = httpService:JSONDecode(jsons)
local first = jsons[1]
-- Create the first object.
local obj = Instance.new(first.ClassName)
first.ClassName = nil
for n, v in pairs(first) do
local value = construct(n, v, obj)
obj[n] = value
end
baseParent = obj
table.remove(jsons, 1)
table.insert(definedParents, first.Parent)
for i, json in pairs(jsons) do
if table.find(definedParents, json.Parent) then
-- Parent exists; add the object to the defined parents list.
table.insert(definedParents, `{json.Parent}.{json.Name}`)
else
-- Parent does not exist; move it to the index after the definition.
local paths = json.Parent:split(".")
local descendant = paths[#paths]
for j=i, #jsons do
if jsons[j].Name == descendant then
jsons[i] = jsons[j + 1]
break
end
end
end
end
end)
return success, error, jsons
end
local function create(lua)
local startTime = os.clock()
local success, fail = pcall(function()
for _, props in pairs(lua) do
local obj = Instance.new(props.ClassName)
obj.Name = props.Name
props.Name = nil
props.ClassName = nil
for n, v in pairs(props) do
if n == "Name" and baseParent:FindFirstChild(v, true) then
cleaned += 1
obj:Destroy()
continue
end
local value = construct(n, v, obj)
obj[n] = value
end
end
end)
if not success then
warn("Error found! Error:", fail)
end
local endTime = os.clock()
print("Created object(s) in:", endTime - startTime)
end
return function(jsons)
local startTime = os.clock()
local iters = 0
local success, reason, refined = refine(jsons[1])
if success then
for _, json in pairs(refined) do
iters += 1
warn("--- JSON #"..iters.." ---")
create(json)
end
local endTime = os.clock()
warn("Imported in:", endTime - startTime)
if cleaned > 0 then
warn("Amount of duplicates removed:", cleaned)
else
warn("No duplicates found. Your model is healthy from that weird bug!")
end
cleaned = 0
baseParent = nil
definedParents = {}
else
warn("JSONs were malformed or there was an error refining the JSONs. Please try again. Error:", reason)
end
end
Example: [multiple JSONs of object properties] each JSON gets put into the “Create” function as a [JSON containing object properties] but suddenly it turns into [one object property]