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

How can I make an inventory saving system for this type of inventory?

So I've been making an inventory for my fishing game, I have created RemoteEvent that will fire whenever player gets new item to his inventory. I did "GUI" inventory with bunch of slots instead of making individual tables for each player. And now I'm stuck at making inventory saving system, I hope someone helps me :)

There's code:

local slots = script.Parent.Inventory.Slots
local run_service = game:GetService("RunService")


local function init()
	for i,v in pairs(slots:GetChildren()) do
		if v.Name == 'Slot' then
			if v.Item.Value == '' and v.Amount.Value == 0 then
				v.Visible = false
			else
				v.Visible = true
				v.ItemLabel.Text = v.Item.Value
				v.AmountLabel.Text = tostring(v.Amount.Value) 
				v.RarityLabel.Text = v.Rarity.Value
			end
		end
	end
end

local function got_item(item)
	for i,v in pairs(slots:GetChildren()) do
		if v.Name == 'Slot' then
			if v.Item.Value == '' and v.Amount.Value == 0 then
				v.Item.Value = item
				v.Amount.Value = 1
				v.Rarity.Value = 'Unspecified'
				break
			elseif v.Item.Value == item and v.Amount.Value > 0 then
				v.Amount.Value = v.Amount.Value + 1
				break
			end
		end
	end
end

run_service.RenderStepped:connect(init)

1 answer

✓ Accepted answer

To save data between sessions use DataStore. A DataStore is a table that persists between sessions and games. To create and modify DataStores you must use SetAsync, UpdateAsync and RemoveAsync.

Since DataStore is a service you need to get it. It is done as the following:

Ex.

local DataStoreService = game:GetService("DataStoreService")

Then you need to get a table to modify

Ex.

local DataStoreService = game:GetService("DataStoreService")

local ExampleStore = DataStoreService:GetDataStore("ExampleData")

SetAsync creates a new value and modifies it to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:SetAsync("ExamplePlayer", 50)

This creates a new value ExamplePlayer and sets it's corresponding value to 50.

UpdateAsync modifies an existing value to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:UpdateAsync("ExamplePlayer", 50)

This changes ExamplePlayer's corresponding value to 50

RemoveAsync removes a value in the array.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:RemoveAsync("ExamplePlayer")

This removes ExamplePlayer and its corresponding value

For more see Data Stores.

2 comments

0

Thank you for your answer, I know exactly how data stores work, but I don't know how to save each of the slot, I must make for i,v loop or something, any ideas?

0

Oh nevermind I figured it out how to do it, thanks for your answer again

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