Datastore2 :Set() not working for boolean values

Hello,

I am trying to learn about Datastore2, as I heard it is rather convenient. However, I have run into an issue when using the :Set() command for a boolean value.

I am using the following function to change the requested data. It seems to work correctly when I am using an integer, but when value is a bool, it fails to update and gives the warning message.
I have also provided the function I use to initially setup the datastore

local defaultTable = {Wins=0, Points=0, Deaths=0, Kills=0, Infections=0,SWins=0,PWins=0,Coins=0,MusicEnabled=true)
DataStore2.Combine(MASTERKEY, "Wins","Kills","Deaths","Infections","PWins","SWins","Coins","MusicEnabled")

function module.ChangeData(player,key,value,increment)
   local data = DataStore2(key, player)
   if increment then
   	local success, err = pcall(function()
   		data:Increment(value)
   	end)
   	if success then print("Successfully incremented: "..key.." by "..value)
   	else warn("Could not increment Data")end
   else
   	local success, err = pcall(function()
   		print(value)
   		data:Set(value)
   	end)
   	if success then print("Successfully updated: "..key.." to "..value)
   	else warn("Could not update Data..")end
   end
end

function SetupData(player)
   local sessionTable = DataStore2(MASTERKEY, player)
   local sessionData = sessionTable:GetTable(defaultTable)
end

Can anyone help explain why this may be happening. I have just started experimenting with Datastore2 so chances are I am implementing it incorrectly.

Hi @ShrekWasMyIdea!

I’m quite new to DS2 myself but I believe I can see what you’re doing wrong here. Try using a dictionary instead of a table. Something like this:

local defaultTable = {
["Wins"] = 0,
["Kills"] = 0,
--...
["MusicEnabled"] = true
}

Also you should never (apparently) gather data by referencing the masterkey like you’ve done here:

local sessionTable = DataStore2(MASTERKEY, player)

This will cause errors in the future. Instead what you may want to do if you want to get all your data at once is set up your data like so:

local defaultTable = {
 playerData = {
  ["Wins"] = 0,
  ["Kills"] = 0,
  --...
  ["MusicEnabled"] = true
 }
}

Then you can call this:
local sessionTable = DataStore2("playerData", player)
And you will have all of the player’s data.

You can then reference the MusicEnabled value like so:
local music = sessionTable["MusicEnabled"] -- by default is true.

Using the second setup mentioned above means you only need to call DataStore2.Combine on that one key.

DataStore2.Combine(MASTERKEY, "playerData")

I personally split my data up a lot more, so I’ll have “Ints” as all my integer values like kills, deaths etc, and then I will have a table for a certain type of skin, I’ll have a preset table, a settings table… So on and so forth. All of these will need to be added into your combine statement.

Your way may well be valid and it might work, it’s not the method I use but there are probably people out there who use it.

2 Likes

@TomsGames I believe he doesn’t want to set the whole table but instead the value itself? I believe he is basically trying to just set that one particular value to true inside/within the table.

Ah, didn’t read the post carefully enough :+1:

He would be able to do this with the DS2 setup I proposed.

local sessionTable = DataStore2("playerData", player)
sessionTable["MusicEnabled"] = false
DataStore2("playerData", player):Set(sessionTable)

This will get the data, change the value to false, and then set it again.

1 Like

@TomsGames I tried changing it to be a dictionary, but I seem to be running into the same issue. I had read that it was not correct to reference the masterkey in that way, but it seemed to function correctly (not that it would in the future, just when I tested it, it seemed to act like one might expect)

Like @3rdhoan123 suggested, I was trying to update a single value inside the combined datastore rather than save a whole table of values of which only one might have changed. I suppose I thought this may reduce risk of losing data. But I might have to resort to this method instead.

I am still confused as to why this shouldn’t work, but perhaps it does come down to incorrectly referencing the masterkey

you can’t increment a bool, that’s illogical.

To alter a boolean datatype use

 DataStore:Set(true) -- or false

you won’t be able to change a bool value using your module.ChangeData function.

1 Like

I use the increment variable in the function as a bool to check if they want to increment the value or not. When calling this function to change a bool value I set increment to be false so it knows to use :Set() instead of :Increment()

You don’t need to wrap your :Set() in a pcall, that’s internally handled by DataStore2.

How are you using that function to set a bool value to the store?

Ah, I didnt know this. Upon removing the pcall I got an error. I had foolishly concatenated the value changed with a string in a print function in OnUpdate() for debugging. Removing this print allowed the Boolean to be saved correctly.

I was using the function by calling `ChangeData(player, “MusicEnabled”, true, false)

1 Like