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


#62

Its more similar to my private datastore module which allows me to store data of virtually any length and it stores them all in one dictionary automatically. It worked by jsoning the table beforehand and subbing it out based on a save size cap and storing it with a name before the json stream starts indicating the next key


#63

I’m not sure you understand how DataStore2 works? Either way, I’ve never seen data in the wild go over the 260k character limit, and if you are, you have serious data saving problems.


#64

Yah, just to put that in comparison, the bible has 3.5 million letters(not including spaces or punctation, that’s over 7% of the bible). 260k characters is quite enough space for the vast majority of things.


#65

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


#66

yeah go on


#67

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}))

#68

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


#69

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


#70

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


#71

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

#72

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.


#73

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)

#74

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


#75

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)

#76

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


#77

When I rejoin nothing is in the inventory.


#78

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


#79

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.


#80

Check the “SaveInStudio” Value in ServerStorage.


#81

Already tried this, got no warnings.