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

The same way you use :Get on anything else, tables are not treated differently.

Whatever this means, it’s not true. Tables are not treated differently.

The same way you would do it with any other value. Tables are not treated differently. I really am not sure how to help you.

cant we just add playerRemoving

What do you mean by this? DataStore2 already saves when the player leaves.

Is there any plugin like Crazyman32’s Datastore Editor for DataStore2?

Is anything out yet? Can I use Crazyman32’s Datastore Editor?

Is it fine to have so much “datastores” in combined datastore? Is there a (suggested) limit or is it fine to have as many as you want?
I have literally everything there that should be saved…

See: Does CrazyMan's DataStore Editor work for DataStore2? - #6 by MFCmaster1234

2 Likes

The only limit is the 260k character limit, but that’s really hard to hit unless you’re doing something crazy like letting people make buildings or something.

1 Like

i cant configure anything in the server in game putting an item and it never saves
by using this module

Hello! I am using the latest version of DataStore2 and I have recently noticed that some people are losing stats when they leave and join back? I am not sure if this is occurring when the player leaves or when they join. I just find it odd that only some people are having this issue? It has just happened to me there now. I left the game and when I joined back I was reset back to the data I had previously?

This use to happen to everyone when I shutdown all servers but I believe I fixed this issue by getting the lastest DataStore2 module. I tested it with 12 players which is a full server. I shutdown the server and their data stayed. But now I am back to square one and I am losing data again?

Just an update. I had another Datastore repeatedly saving every 60 seconds so that I could do live Leaderboard updates. After about 30 minutes of the server running this datastore starts to fill requests which slowed down the saving process for when a player leaves by 10 seconds.

I think this might have been why data was being lost. I’ve disabled the leaderboards and working on a more efficient way to display them!

1 Like

These are issues with your code, not mine. I apologize, but there’s really not much I can help you out with here.

1 Like

this actually has been happening to a lot of people who use DS2 (the older versions work just fine with the same code) I didn’t know what the issue was, but on another note, you need to add a table of ‘saved’ players… So basically after autosaving a player then the players ‘saved’ value is set to ‘true’ then it’ll wait 6 seconds using spawn() (or coroutine) and set it back to false automatically, allowing you save them again.

local saveInterval = 1
--pseudo code
if(saved[player.Name] == false) then
    local success = save(player)
    if(success==true) then
        saved[player.Name] = true
        spawn(function()
            wait(6)
            saved[player.Name] = false
        end)
    end
else
    print("Player was already saved recently")
end

this will allow you to run through and save all the ‘unsaved’ players way faster without filling up your queue. (don’t copy and paste this, its just pseudo code)

3 Likes

260k character limit per player or per game?
Also, per datastore or per all datastores in game

1 Like

I believe its around 260k characters per key.

Ok, I’ve returned and I’ve made some progress on saving/loading tables.
I have figured out saving/loading, kind of. I think there is an issue with Saving, because printing values gives me the same bug.

Heres the script, but the issue is that whenever rejoining, no matter what items were in your inventory, multiples of the 2 same items will appear. Here is the script, I have a feeling its just me being stupid and missing the obvious, but I can’t figure it out.

local players = game:GetService("Players")

local DS2 = require(game.ServerScriptService.DataStore2)

game.Players.PlayerAdded:Connect(function(player)
	wait(3)
	local saveslottimer = Instance.new("NumberValue", player)
	saveslottimer.Name = "SaveSelector"
	saveslottimer.Value = 0
	repeat
		wait()
	until saveslottimer.Value == 1 
	
	local saveslot = player.SaveSlot
	
	
	local stats = Instance.new("Folder", player)
	stats.Name = "Stats"
		
	local money = Instance.new("NumberValue", stats)
	money.Name = "Money"

	local er = Instance.new("NumberValue", stats)
	er.Name = "Eridium"
	
	local level = Instance.new("NumberValue", stats)
	level.Name = "Level"
	
	local exp = Instance.new("NumberValue", stats)
	exp.Name = "Experience"
	
	local maxexp = Instance.new("NumberValue", stats)
	maxexp.Name = "MaxExperience"
	
	local inv = Instance.new("Folder", player)
	inv.Name = "Inventory"
	
	DS2.Combine(saveslot.Value, "Money", "Eridium", "Level", "Experience", "MaxExperience", "Inventory")
	
	local moneystore = DS2("Money", player)
	local erstore = DS2("Eridium", player)
	local levelstore = DS2("Level", player)
	local expstore = DS2("Experience", player)
	local maxexpstore = DS2("MaxExperience", player)
	local invstore = DS2("Inventory", player)
	
	local invholder = {}
	
	money.Value = moneystore:Get(500)
	er.Value = erstore:Get(0)
	level.Value = levelstore:Get(1)
	exp.Value = expstore:Get(0)
	maxexp.Value = maxexpstore:Get(500)
	invholder = invstore:Get()
	
	wait(1)
	
	saveslottimer.Value = 2
	
	money.Changed:Connect(function()
		moneystore:Set(money.Value)
	end)
	er.Changed:Connect(function()
		erstore:Set(er.Value)
	end)
	level.Changed:Connect(function()
		levelstore:Set(level.Value)
	end)
	exp.Changed:Connect(function()
		expstore:Set(exp.Value)
	end)
	maxexp.Changed:Connect(function()
		maxexpstore:Set(maxexp.Value)
	end)
		
	local function SaveInv()
		invholder = {}
		for i,v in pairs(inv:GetChildren()) do
			local realitem = game.ReplicatedStorage.Items[v.Name]
			local required = require(realitem.Info.Information)
			invholder[tostring(required.Id)] = v.Value
		end
		return invholder
	end
	
	inv.ChildAdded:Connect(function()
		SaveInv()
		wait(.5)
		invstore:Set(invholder)
	end)
	inv.ChildRemoved:Connect(function()
		SaveInv()
		wait(.5)
		invstore:Set(invholder)
	end)
		
	wait(1)	
		
	if invholder == nil then
		local item1 = Instance.new("NumberValue")
		item1.Name = "Stone Mine"
		item1.Value = 3
		item1.Parent = inv
		local item2 = Instance.new("NumberValue")
		item2.Name = "Basic Furnace"
		item2.Value = 1
		item2.Parent = inv
		local item3 = Instance.new("NumberValue")
		item3.Name = "Basic Conveyor"
		item3.Value = 10
		item3.Parent = inv
	else
		for i,v in pairs(invholder) do
			for i,a in pairs(game.ReplicatedStorage.Items:GetChildren()) do
				local required = require(game.ReplicatedStorage.Items[a.Name].Info.Information)
				if required.Id == tonumber(i) then
					local item = Instance.new("NumberValue")
					item.Name = a.Name
					item.Value = v
					item.Parent = inv
				end
			end
		end
	end
end)

The bits about ‘required’ is just a module script with info about each item in it. Each item has its own ID, which it saves, as well as the value.

Can I save tools like flash or True values like a bool value

Also does the module have limits

Data:Get(false) --or true
Data:Ser(true) --or false

1 Like

yes, you can save String, Number or Boolean values into a datastore.

(this also includes tables that contain String, Number and/or Boolean Values)

Realistically you could save anything except threads, userdatas and functions. And the reading/writing logic would be the exact same.