Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a better way to save an infinite inventory?

local datastore = game:GetService("DataStoreService"):GetDataStore("Inventory")
game.Players.PlayerAdded:connect(function(plr)
	local key = "user_" .. plr.UserId
	local items = datastore:GetAsync(key) or {
		Slot1 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot2 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot3 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot4 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot5 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot6 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot7 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot8 = {
			item = "",
			amount = 0,
			image = 0
		}
	}
	local inv = plr.PlayerGui:WaitForChild("Inventory").InvFrame:GetChildren()
	for i,v in pairs(inv) do
		if not v:IsA("Script") then
			v.Item.Value = items[v.Name].item
			v.Amount.Value = items[v.Name].amount
			v.ImageId.Value = items[v.Name].image
			if v.Item.Value ~= nil and v.Amount.Value ~= 0 and v.ImageId.Value ~= 0 then
				v.ItemImage.Image = "http://www.roblox.com/asset/?id=" .. v.ImageId.Value
				v.ItemAmount.Text = v.Amount.Value
				v.ItemLabel.Text = v.Item.Value
			end
		end
	end
	local function save()
		datastore:SetAsync(key, items)
	end
	for i,v in pairs(plr.PlayerGui.Inventory.InvFrame:GetChildren()) do
		if not v:IsA("Script") then
			v.Amount.Changed:connect(function()
				for index,value in pairs(items) do
					if index == v.Name then
						value.amount = v.Amount.Value
						save()
					end
				end
			end)
			v.Item.Changed:connect(function()
				for index,value in pairs(items) do
					if index == v.Name then
						value.item = v.Item.Value
						save()
					end
				end
			end)
			v.ImageId.Changed:connect(function()
				for index,value in pairs(items) do
					if index == v.Name then
						value.image = v.ImageId.Value
						save()
					end
				end
			end)
		end
	end
end)
game.Players.PlayerRemoving:connect(function(plr)
	local key = "user_" .. plr.UserId
	local items = datastore:GetAsync(key) or {
		Slot1 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot2 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot3 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot4 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot5 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot6 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot7 = {
			item = "",
			amount = 0,
			image = 0
		},
		Slot8 = {
			item = "",
			amount = 0,
			image = 0
		}
	}
	for i,v in pairs(plr.PlayerGui.Inventory.InvFrame:GetChildren()) do
		if not v:IsA("Script") then
			items[v.Name].item = v.Item.Value
			items[v.Name].amount = v.Amount.Value
			items[v.Name].image = v.ImageId.Value
		end
	end
	datastore:SetAsync(key, items)
end)

As you can see, I list every slot 1-8 in the DataStore table, is there anyway where I don't have to do that, and can just have an infinite inventory save without having to keep on adding Slots to the table?

Help appreciated, thanks!

1 answer

Just use an array.

You don't need to give the slots "names" like .Slot1, .Slot2, just use [1] [2]`, etc.

When you need a new slot, just add it in. You don't need them all to be there at the beginning.

1 comment

0

Example or wiki link by chance?

This discussion is preserved as it was. New answers, comments, and votes are closed.