Datastore2 Problem

So, lately i’ve added data saving in a game im developing with my friends. The datastore saves your current loadout and what you have bought. The second part works fine, however, sometimes it doesnt save your loadout.
I’m adding here the parts with the script that sets up datastores

Datastore2 Set up

–T = Loadout (Not working properly), AT = What you’ve bought (Works properly)
–Note = i’m setting slots to “nil” to make it a string (for some reason it didnt work as nil)

local function setupUserData()
	local userData = {	
	
    AT = {
	Sniper = false,
	Soldier = false,
	MG = false,
	Bank = false,
	SMG = false,
	Demolitionist = false
    };
    T = {
    T1 = "nil",
    T2 = "nil",
    T3 = "nil",
 	    T4 = "nil",
    T5 = "nil"
        };

	}
	
	return userData
end

local userData = Datastore2(userDataStoreName, plr):GetTable(setupUserData())
Equip function
function Equip()
	
	local T1V = script.Parent.Parent.Parent.Parent.Parent.Slots.T1.T
	local T2V = script.Parent.Parent.Parent.Parent.Parent.Slots.T2.T
	local T3V = script.Parent.Parent.Parent.Parent.Parent.Slots.T3.T
	local T4V = script.Parent.Parent.Parent.Parent.Parent.Slots.T4.T
	local T5V = script.Parent.Parent.Parent.Parent.Parent.Slots.T5.T
	
	if T1V.Value == nil or T1V.Value == "nil" then
		userData["T"]["T1"] = Title.Text
		T1V.Value = Title.Text
		script.Parent.Parent.Parent.Parent.Parent.Slots.T1.Image = script.Parent.Parent.Parent.Parent.Parent.ScrollingFrame.Towers[Title.Text].Image
		Datastore2(userDataStoreName, plr):Set(userData)
	elseif T2V.Value == nil or T2V.Value == "nil" and T1V.Value ~= nil and T1V.Value ~= Title.Text and T2V.Value ~= Title.Text and T3V.Value ~= Title.Text and T4V.Value ~= Title.Text and T5V.Value ~= Title.Text  then
		userData["T"]["T2"] = Title.Text
		T2V.Value = Title.Text
		Datastore2(userDataStoreName, plr):Set(userData)
		script.Parent.Parent.Parent.Parent.Parent.Slots.T2.Image = script.Parent.Parent.Parent.Parent.Parent.ScrollingFrame.Towers[Title.Text].Image
	elseif T3V.Value == nil or T3V.Value == "nil" and T2V.Value ~= nil and T1V.Value ~= nil and T1V.Value ~= Title.Text and T2V.Value ~= Title.Text and T3V.Value ~= Title.Text and T4V.Value ~= Title.Text and T5V.Value ~= Title.Text  then
		userData["T"]["T3"] = Title.Text
		T3V.Value = Title.Text
		Datastore2(userDataStoreName, plr):Set(userData)
		script.Parent.Parent.Parent.Parent.Parent.Slots.T3.Image = script.Parent.Parent.Parent.Parent.Parent.ScrollingFrame.Towers[Title.Text].Image
	elseif T4V.Value == nil or T4V.Value == "nil" and T3V.Value ~= nil and T2V.Value ~= nil and T1V.Value ~= nil and T1V.Value ~= Title.Text and T2V.Value ~= Title.Text and T3V.Value ~= Title.Text and T4V.Value ~= Title.Text and T5V.Value ~= Title.Text  then
		userData["T"]["T4"] = Title.Text
		T4V.Value = Title.Text
		Datastore2(userDataStoreName, plr):Set(userData)
		script.Parent.Parent.Parent.Parent.Parent.Slots.T4.Image = script.Parent.Parent.Parent.Parent.Parent.ScrollingFrame.Towers[Title.Text].Image
	elseif T5V.Value == nil or T5V.Value == "nil" and T4V.Value ~= nil and T3V.Value ~= nil and T2V.Value ~= nil and T1V.Value ~= nil and T1V.Value ~= Title.Text and T2V.Value ~= Title.Text and T3V.Value ~= Title.Text and T4V.Value ~= Title.Text and T5V.Value ~= Title.Text then
		userData["T"]["T5"] = Title.Text
		T5V.Value = Title.Text
		Datastore2(userDataStoreName, plr):Set(userData)
		script.Parent.Parent.Parent.Parent.Parent.Slots.T5.Image =        script.Parent.Parent.Parent.Parent.Parent.ScrollingFrame.Towers[Title.Text].Image
	end
	Datastore2(userDataStoreName, plr):Save()
	debounce = false
	
end

I also wanted to specify that this error occurs sometimes, not every time a player joins the game.

2 Likes

There isn’t much information on this thread. What error are you encountering? What have you tries in order to diagnose this? Have you checked the already-extensive DataStore2 documentation thread for issues similar to yours?

2 Likes

The error is that sometimes (about 10%) when you join a game it doesnt load your loadout, but this error never occured with the other part of the datastore. I’ve tried reducing the amount of times :Save() is called and i tried replacing :Get() with :GetTable() (this actually helped a bit). I’ve read a lot of topics about similiar issues, but i couldnt find a solution to my problem.

2 Likes

If you are testing in studio ensure that you have a boolean in ServerStorage that is called SaveInStudio and is set to true.

Another possible issue if you are in studio is that the onPlayerJoined event doesn’t fire, this is a studio bug that is known however has not been fixed. An easy way to check this is to put a print in the event and if it does not print then the even has not fired. Just simply stop and play again until it works - N.B. This is not a problem on the main game.

As stated on the DS2 API that can be found here:

Get() - Will return the value cached in the data store, if it exists. If it does not exist, will then attempt to get the value from Roblox data stores. This function will only yield if there is no value in the data store.

This means if there is data that is saved, it will get it otherwise it will use the default value that is parsed.

GetTable() - Will get the value (either from the cache or Roblox data stores), and patch it with the default value if it doesn’t have the keys.

This means that it will check if there is data for ALL the keys in the table unlike get that will just see if a table is saved. This means you can add a key later after it has saved, so anyone with saved data will get the default for that key unlike if you used get then they will not even get the key.

Your best chance if this has not answered it is to ask directly on the DS2 thread because the author of DS2 - @Kampfkarren is active there and answers most questions

3 Likes

But if :GetTable() is causing the problem, why it works sometimes?

2 Likes

No, :GetTable() is not the problem from what I understand, I just put what it did because from your reply:

i tried replacing :Get() with :GetTable() (this actually helped a bit)

I thought you didn’t fully understand how it worked - Sorry for the confusion :smiley:
Is is possible for you to put a repro file here so it is easier to troubleshoot the problem?

2 Likes

Oh, ok. The game is open for testing right now, if you want i give you a link to it to test (the game is made of 2 different places so giving files would be hard)

3 Likes