Hello. I need to make a system where i can store parts inside of tables. I really need this, so please help me out. It is something like using :Clone() but you use functions to get the data of a model and then re-construct it without using :Clone(). Only tables.

Now my problem here is: The cloned instance parent is the same of the uncloned, normal instance. This means that there are two same instances in the same parent. Take a look at this picture: https://ibb.co/8rJJ9ng

This is my actual desired result: https://ibb.co/7WhfHfk

Code and a PS: My try is in the function ConstructModelWithData() with the commented out block. Try enabeling it, and it does nothing.

The module variable is a module/script i found online, the link is here:https://scriptinghelpers.org/questions/50784/how-to-get-list-of-object-properties or here:https://web.roblox.com/library/5824477121/test

Code:

local module = require(script.GetProperties)

local ClassProperties = module.ClassProperties
function GetInstanceData(instance) 
	local properties = ClassProperties[instance.ClassName]
	local data = {}
	for i, v in pairs(properties) do
		if instance[v] then
			data[v] = instance[v]
		end
	end
	data["ClassName"] = instance.ClassName
	return data
end

function HasProperty(object, prop)
	local succes = pcall(function()
		local t = object[prop]
	end)
	return succes
end

function ChangeProperty(object, prop, val)
	local succes = pcall(function()
		if prop == "Position" then
			object[prop] = val + Vector3.new(0,100,0)
		else
			object[prop] = val
		end
	end)
	return succes
end

function GetModelData(model)
	local children = model:GetChildren()
	print(table.unpack(children))
	local data = {}
	local function GetDescendantsModel(model2)
		for i, v in pairs(model2:GetDescendants()) do
			if model:FindFirstChild(v) then
				local iData = GetInstanceData(v)
				iData.Parent = game.Lighting
				data[#data+1] = iData
			else
				local iData = GetInstanceData(v)
				data[#data+1] = iData
			end
		end
	end
	GetDescendantsModel(model)
	return {data = data, model1 = model}
end

function IsTheSame(o1, model)
	for i, v in pairs(model:GetDescendants()) do
		if o1 == v then
			return v
		end
	end
	return false
end

function ConstructModelWithData(dataTable)
	local dataTable1 = dataTable.data
	local modell = dataTable.model1
	local model = {}
	for i, v in pairs(dataTable1) do
		local instance = Instance.new(v.ClassName)
		for i2, v2 in pairs(v) do
			if HasProperty(instance, i2) then
				if ChangeProperty(instance, i2, v2) then
					model[#model+1] = instance
				end
			end
		end
	end 
	--[[ the code i want you to fix
	for i, v in pairs(model) do
		local is = IsTheSame(v, modell)
		if is ~= false then
			if is.Parent ~= workspace then
				local p = modell:GetDescendants()
				if p[v.Parent] ~= nil then
					print("no!")
					v.Parent = p[v.Parent.Parent]
				end
			else
				v.Parent = script
			end
		end
	end
	--]]
	return model
end

data = GetModelData(workspace.Microwave)
modelC = ConstructModelWithData(data)

2 comments

0

You should rename model and modell. It will get confusing. I always try to write in a way so if I hand someone person my code, they will understand what the variables are, what the functions do, etc..

0

yeah it happens sometimes, my codes will get confusing. Because of a simple error like that a table gets overwritten.