How to use DataStore2 - Data Store caching and data loss prevention

Hey, I’m really confused on how to add items to the players inventory. Anyone mind helping? :smile:

2 Likes

yeah go on

14 Likes

So, I am not entirely sure what table I need to add my items to. Here is my code:

local DefaultInventory = {
	["Default"] = 1
}

Players.PlayerAdded:Connect(function(Plr)
	
	-- // Table Inventory
	
	local TableInventory = DataStore2("TableInventory3", Plr)
	
	TableInventory:SetBackup(3)
	
	TableInventory:BeforeInitialGet(function(Inventory)
		
		local Deserialized = {}
		
		for _, ItemId in pairs(Inventory) do
			for Key, Value in pairs(DefaultInventory) do
				if Value == ItemId then
					table.insert(Deserialized, Key)
				end
			end
		end
		
		return Deserialized
		
	end)
	
	TableInventory:BeforeSave(function(Inventory)
		
		local Serialized = {}
		
		for _, ItemName in pairs(Inventory) do
			table.insert(Serialized, DefaultInventory[ItemName])
		end
		
		return Serialized
		
	end)
	
	local function CallInvEvent(Value)
		ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, TableInventory:GetTable({DefaultInventory}))
	end
	
	CallInvEvent(TableInventory:GetTable({DefaultInventory}))
9 Likes

Whenever you need to give a player an item, all you should need to do is Get, insert into the table, then Set.

10 Likes

Have you ever thought of branding datastore2(For example call it Safe Saving) for the use of players?

6 Likes

I haven’t thought about it until now but that doesn’t sound like something I’d be interested in.

6 Likes

I’ve tried this but it still doesn’t want to update the inventory.

ReplicatedStorage.DataEvents.Unboxed.OnServerEvent:Connect(function(Plr, Item)
	
	local TableInventory = DataStore2("TableInventory5", Plr)
	
	local NewTable = TableInventory:GetTable({})
	
	table.insert(NewTable, Item)
	
	TableInventory:Set({NewTable})

    for i, v in pairs(TableInventory:GetTable({})) do -- // Doesn't print anything I just added.
	
		print(i)
		print(v)
	
	end

end)
-- // Table Inventory
	
	local TableInventory = DataStore2("TableInventory5", Plr)
	
	TableInventory:SetBackup(3)
	
	TableInventory:BeforeInitialGet(function(Inventory)
		
		local Deserialized = {}
		
		for _, ItemId in pairs(Inventory) do
			for Key, Value in pairs(DefaultInventory) do
				if Value == ItemId then
					table.insert(Deserialized, Key)
				end
			end
		end
		
		return Deserialized
		
	end)
	
	TableInventory:BeforeSave(function(Inventory)
		
		local Serialized = {}
		
		for _, ItemName in pairs(Inventory) do
			table.insert(Serialized, DefaultInventory[ItemName])
		end
		
		return Serialized
		
	end)
	
	local function CallInvEvent(Value)
		ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, Value)
	end
	
	CallInvEvent(TableInventory:GetTable({DefaultInventory}))

	for i, v in pairs(TableInventory:GetTable({})) do
	
		print(i)
		print(v)
	
	end
6 Likes

You’re setting the data to a table of your table. Try just using :Set(NewTable).

Also, :GetTable({}) is redundant, just use :Get({}) at that point. GetTable is only there for the default values.

7 Likes

Doesn’t seem to work, updated code:

PlayerAdded:Connect(function(Plr)

    -- // Table Inventory
	
	local TableInventory = DataStore2("TableInventory7", Plr)
	
	TableInventory:SetBackup(3)
	
	TableInventory:BeforeInitialGet(function(Inventory)
		
		local Deserialized = {}
		
		for _, ItemId in pairs(Inventory) do
			for Key, Value in pairs(DefaultInventory) do
				if Value == ItemId then
					table.insert(Deserialized, Key)
				end
			end
		end
		
		return Deserialized
		
	end)
	
	TableInventory:BeforeSave(function(Inventory)
		
		local Serialized = {}
		
		for _, ItemName in pairs(Inventory) do
			table.insert(Serialized, DefaultInventory[ItemName])
		end
		
		return Serialized
		
	end)
	
	local function CallInvEvent(Value)
		ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, Value)
	end
	
	CallInvEvent(TableInventory:GetTable({DefaultInventory}))

	for i, v in pairs(TableInventory:Get({})) do
	
		print(i)
		print(v)
	
	end
end)

ReplicatedStorage.DataEvents.Unboxed.OnServerEvent:Connect(function(Plr, Item)
	
	local TableInventory = DataStore2("TableInventory7", Plr)
	
	local NewTable = TableInventory:Get({})
	
	table.insert(NewTable, Item)
	
	TableInventory:Set(NewTable)

end)
9 Likes

Change this to :GetTable(DefaultInventory) and try again. You do it a couple of times it seems.

4 Likes

Done that, but still isn’t adding anything to the inventory.

PlayerAdded:Connect(function(Plr)

-- // Table Inventory
	
	local TableInventory = DataStore2("TableInventory9", Plr)
	
	TableInventory:SetBackup(3)
	
	TableInventory:BeforeInitialGet(function(Inventory)
		
		local Deserialized = {}
		
		for _, ItemId in pairs(Inventory) do
			for Key, Value in pairs(DefaultInventory) do
				if Value == ItemId then
					table.insert(Deserialized, Key)
				end
			end
		end
		
		return Deserialized
		
	end)
	
	TableInventory:BeforeSave(function(Inventory)
		
		local Serialized = {}
		
		for _, ItemName in pairs(Inventory) do
			table.insert(Serialized, DefaultInventory[ItemName])
		end
		
		return Serialized
		
	end)
	
	local function CallInvEvent(Value)
		ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, Value)
	end
	
	CallInvEvent(TableInventory:GetTable(DefaultInventory))
	
end)

ReplicatedStorage.DataEvents.Unboxed.OnServerEvent:Connect(function(Plr, Item)
	
	local TableInventory = DataStore2("TableInventory9", Plr)
	
	local NewTable = TableInventory:Get({})
	
	table.insert(NewTable, Item)
	
	TableInventory:Set(NewTable)

end)
8 Likes

How do you know it’s not being added to the inventory? You only call CallInvEvent the first time.

4 Likes

When I rejoin nothing is in the inventory.

11 Likes

Have you checked your output to make sure it’s actually saving? DataStore2 gives warnings when it intentionally doesn’t save.

9 Likes

Didn’t get a warning in studio.

EDIT: SaveInStudio false I got this - 21:51:06.706 - Data store TableInventory9 attempted to save in studio while SaveInStudio is false.

7 Likes

Check the “SaveInStudio” Value in ServerStorage.

5 Likes

Already tried this, got no warnings.

7 Likes

Can you PM me on this? Realized might be spamming the thread a bit.

7 Likes

In studio in play solo mode if I press the stop button it freezes for about 8 seconds due to the BindOnClose or something in the module when it saves in studio. Any way to stop this or do I just have to not make it save in studio using the variable?

image

(Keep in mind i am using the module when i took it from your inventory and took it like 1 month after the module came out so mine might be outdated)

10 Likes

Right now you just have to make it not save in studio until the BindToClose studio patch is rolled out. Sorry for the inconvenience.

6 Likes