How to store and load Dictionaries inside a table?

I’m trying to make a datastore system. I need help storing data in a certain way, example below:

local data = [
{Name=“Item”, Amount=2,Type=“Object”},
{Name=“etc”}
]

If I just do that and return data, then will it work?

and also stuff like hotbars:
local hotbar = [
{Slot=1,Item=“EX”,Amount=2,type=Object}, {Slot=2, Item=“EX2”, Amount=1, Type=Weapon}
]

would organizing them like that and saving the table work?
and I just load it like:


local Hotbar = []
for i,v in pairs(hotbar) do
table.insert(Hotbar,v)
end
game.ReplicatedStorage.LoadHotbar:FireClient(“ClientUser”, Hotbar)


lmk if doing it that way would work, I haven’t used tables, arrays, and datastores for a while. Also, please excuse my poor writing, I’m kinda in a rush.

change [] to {}

local Hotbar = {}
for i,v in pairs(hotbar) do
    table.insert(Hotbar, v)
end
game.ReplicatedStorage.LoadHotbar:FireClient("ClientUser", Hotbar)

you mentioned using datastores, but in your code, you’re just using local tables. have you implemented roblox’s datastore system?

This is just in theory, I got the actual saving part. I just haven’t used tables and dictionaries in a long time, Thanks.

TABLE:

local data = {
	
	{
		
		['Name'] = 'Item',
		['Type'] = 'Object',
		['Amount'] = 2,
		
	}
	
}

for _, itemInTable in pairs(data) do
	
	print(itemInTable)
	
	--[[ 
	
	WOULD PRINT:
	
	{

		['Name'] = 'Item',
		['Type'] = 'Object',
		['Amount'] = 2,

	}
	
	-- ]]
	
end

DICTIONARY:

local data = {
	
	['slot_1'] = {
		
		['Name'] = 'Item',
		['Type'] = 'Object',
		['Amount'] = 2,
		
	}
	
}

for slotNumber, itemInTable in pairs(data) do
	
	print(slotNumber)
	print(itemInTable)
	
	--[[ 
	
	WOULD PRINT:
	
	slot_1
	
	{

		['Name'] = 'Item',
		['Type'] = 'Object',
		['Amount'] = 2,

	}
	
	-- ]]
	
end

They work in the same way, in the first case the index of the value is set my roblox, which defaults to numbers counting up, but in the second case you are forcing the index to be something specific.

I would suggest taking a look at the link below, it will help a lot with your issue! :slight_smile:

1 Like