Folder returns table when cloned

I’m trying to clone a folder with various features that’ll help me store custom item data for the player. But everytime I clone the template it returns it as a table value instead.

image

local function itemDataToObject(itemData)
	
	print(script.Objects.DataSlot) -- 1

	local DataSlotObject = script.Objects.DataSlot:Clone()

	print(DataSlotObject) -- 2

	return DataSlotObject;
end

Returns

1:	DataSlot
2:	table: 0x2f54cea2400197cd -- is random each time

I’d prefer to clone the template rather then rebuild it each item because there are a LOT of objects in it and I wanna reduce the impact on performance.

Please let me know if more information is needed!

1 Like

Try running

print(DataSlotObject:GetChildren())

and let me know how that works out for you. Attempting to recreate this in my own place and it’s just returning the name of the Folder rather than table that you’re getting. Could be something to do with the size of the overall instance itself being condensed down into a table of instance references due to the number of children for the sake of memory conservation. It’s likely nothing more than cosmetic.

1 Like

Yes it seems to print the children fine. Thankfully.

I’ll be taking another quick look in a bit but as you said it looks to just be cosmetic.

All good, don’t be afraid to ask for more guidance if anything pops up further on down the line. I’ll be online for the next 2 or so hours.

1 Like

It all seems to be working well enough, but I do have a quick question about dictionaries if that’s cool.

I’m trying to get the key of the item selected. While it may seem as simple as turning the index into a string to mimic it, everytime I move up a digit the keys add a zero to the left. So at 10, 1 turns into 01–and at 100, 1 turns into 001.

for _, inventory in pairs(lastInventory.Equipped) do
		
	print(inventory) -- 1

	for index, item in pairs(inventory) do

		print(item) -- 2

		local newItem = itemDataToObject(item)

	end

end

Returns

1:	{
	["01"] =  ▶ {...},
	["02"] =  ▶ {...},
	["03"] =  ▶ {...},
	["04"] =  ▶ {...},
	["05"] =  ▶ {...},
	["06"] =  ▶ {...},
	["07"] =  ▶ {...},
	["08"] =  ▶ {...},
	["09"] =  ▶ {...},
	["10"] =  ▶ {...}
}

2:	{
	["Empty"] = true,
	["Item"] =  ▶ {...}
}

I know of a workaround but I was wondering if there was a quick way to get the key instead.

As far as I’m aware, there’s no simple way to really get the key of an item within dictionaries, at least not within Roblox. One method I used to grab the key given a specific input was really just to iterate over every item in the dictionary in pairs until my “v” is equal to the item I’m looking for. Such as this:

local Dict = {
    ["a"] = 1,
    ["b"] = 2,
    ["c"] = 3,
    ["d"] = 4,
    ...
}

local function GetKeyFromItemInDictionary(Dict, Value)
    for k, v in pairs(Dict) do    
        if v == Value then
            return k
        end
    end
end

print( GetKeyFromItemInDictionary(Dict, 1) )
> "a"
print( GetKeyFromItemInDictionary(Dict, 3) )
> "c"

Hope this helps, was a little confused about the wording of your question, so if I’m misinterpreting that, lemme know what you meant.

1 Like

I’ll look into using this, and thank you for your help!

Edit: I figured out a great way to adapt this into it.

I changed my original for each loop from this:

for _, inventory in pairs(lastInventory.Equipped) do
		
	print(inventory)
	-->> table: 0x2f54cea2400197cd

	for index, item in pairs(inventory) do

		print(item)
		-->> { item =  ▶ {...}, Empty }

		local newItem = itemDataToObject(item)

	end

end

to this:

for _vkey, inventory in pairs(lastInventory.Equipped) do
		
	print("inventory: " .. _vkey)
	-->> inventory: Equipment

	for _ikey, item in pairs(inventory) do

		print("item: " .. _ikey)
		-->> item: 001

		local newItem = itemDataToObject({_ikey, item}) -- Key/Name and the itemData

	end

end

Good to hear. If you have any other programming concerns, I’m on here somewhat frequently. Feel free to ping me a message on here and I’ll be happy to help :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.