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

Say if I make a tool for a simulator or make a developer product instead of saying: player.leaderstats.Points.Value, would I say: Increment(“points”, player)?

Idk why this characters script isn’t saving. It saves the characters, but not the points. I get the message saying that it did save, but its not. Help please?

local DataStore2 = require(game:GetService("ServerScriptService").DataStore2)
local CombinedDataStore = DataStore2.Combine("Characters", "CharactersBackup")
local MarketplaceService = game:GetService("MarketplaceService")

local DefaultTable = {
	Characters = {""},
	EquippedCharacter = "",
	Leaderstats = {{Points = 50}}
}

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local DataStore = DataStore2("Characters", Player)

	local DataTable = DataStore:GetTable(DefaultTable)

	local Data = Instance.new("Folder")
	Data.Name = "Characters"
	Data.Parent = Player

	for DataType, Value in pairs(DataTable.Characters) do
		local Character = Instance.new("BinaryStringValue")
		Character.Name = Value
		Character.Parent = Data
	end
	
	Player:SetAttribute("EquippedCharacter", DataTable.EquippedCharacter)

	for DataType, Value in pairs(DefaultTable.Characters) do
		if not Data:GetAttribute(DataType) then
			Data:SetAttribute(DataType, Value)
		end
	end
	
	local changing = Instance.new("BoolValue")
	changing.Name = "ChangingChar"
	changing.Parent = Player
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "Leaderstats"
	Leaderstats.Parent = Player
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = DataTable.Leaderstats.Points
	Points.Parent = Leaderstats
	
	
	
	local function SaveData()
		local DataToSave = {
			Characters = {},
			EquippedCharacter = "",
			Leaderstats = {{}}
		}

		for Number, Value in pairs(Data:GetChildren()) do
			DataToSave.Characters[Number] = Value.Name
		end

		DataToSave.EquippedCharacter = Player:GetAttribute("EquippedCharacter")

		DataStore:Set(DataToSave)
	end
	Data.Changed:Connect(SaveData)
	Player:GetAttributeChangedSignal("EquippedCharacter"):Connect(function()
		SaveData()
	end)
	Points.Changed:Connect(SaveData)
	GetCharacter(Player)
end)

Also, the functions in here are functions to load the character.

Could you explain some ways to do this without session locking? Including methods outside of roblox, since I couldn’t find much on it.

1 Like

I recently came across a bug which allows players to dupe in my game.
If a player crashes themselves their data wont save. (using the newest version too)

Should I use DataStore2.SaveAll on player removing to prevent this?

You’re bad on luck, DataStore2 doesn’t handle data duplications primarily because of no session lock features. Any of the methods you mentioned won’t work, and if you really want a proper data management which handles many nasty edge cases, I highly recommend using ProfileService.

1 Like

It would be very tedious to change all my code to introduce profileservice.

yo do u know how we could see if the player has played in another datastore like before a datawipe

so like
if (player has played in another save) then
– do the thing i want to do
end

Could there still be entries in the DataStore that are not listed in the OrderedDataStore due to a failure to write to the OrderedDataStore?

Maybe. Swapping the two RemoveAsync should clear that worry though.

@Kampfkarren

Hey. I really enjoy using your convenient module.

However, I found this a little hard to understand, as I’m using an updated version of the plugin. I’m using a combined data store, and I need to edit one of its values.

I know what my key is, and I toggled the correct settings. This is what I found:

image

I’ve noted this value down, but I’m not sure where to enter this information.

Could you help? Thanks!

1 Like

Hello, I don’t understand Luay very well, can you tell me how to configure the module so that it automatically sends data to the client.

Just attach it to a RemoteEvent

1 Like

Question what If I need to send something from a local script but can’t cause you can Require the module from the client it always says “DataStore2 isn’t a valid member of ServerScriptService”

Quick question:
How would I loop through all of the datastores under a certain player?

Why it doesn’t make sense, my game is slowing down a lot because of onUpdate
(sorry for such a long period of time)

My game when player leave it save but when game shutdowned it doesnt save why? I used the latest datastore2 module from github, i tried to use SaveAll loop make more broken like when the server getting slower when leave it wont save the data :frowning:

Would you be able to give an example on how “:GetTable()” is supposed to work?

Im trying to add a table array of stats (Tools owned) into my data, but only it has worked for me. It errors every other time. The code I used:

local function UpdateGears(Val)
		GearTable = GearStore:GetTable(Val)
		GearsFolder:ClearAllChildren()
		for _,v in ipairs(GearTable) do
			local gValue = Instance.new("BoolValue")
			gValue.Name = v
			gValue.Parent = GearsFolder
		end
	end

for _,v in pairs(GearStore:GetTable("Gears")) do
		if not plr.Backpack:FindFirstChild(v) or not plr.StarterGear:FindFirstChild(v) then
			local clone = Gears[v]:Clone()
			local clone2 = Gears[v]:Clone()
			clone.Parent = plr.Backpack
			clone2.Parent = plr.StarterGear
		end
	end

The error id get is:

invalid argument #1 to ‘pairs’ (table expected, got string)

Any help is appreciated!

Nothing in what you’re doing makes sense to be using GetTable. Why not just use Get() instead? It works on tables.

1 Like

will this module work good

according to QuickNetwork - A powerful hybrid alternative to ProfileService and DataStore2

who seems like a guy who’s over boasting who said this is outdated and even you admitted

should i use this over profileservice and Quicknetwork

–skyquest

is anyone going to answer this :frowning: