BoolValue in DataStore not saving

  1. What do you want to achieve? Create a BoolValue that saves using DataStore

  2. What is the issue? The BoolValue doesn’t save while the other IntValue does

  3. What solutions have you tried so far? I have been debugging this for a while and tried googling for an answer.

The “money” IntValue saves without any errors however the BoolValue “OwnsObamaFigure” does not save.

-- SETTINGS --
local money_name = "Money"
local autosave_cooldown = 10
--------------

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Money")
local gearStore = DS:GetDataStore("Gear")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
local remote2 = game:GetService("ReplicatedStorage").Remotes.RemoveCurrency

game.Players.PlayerAdded:Connect(function(player)
	
	local moneyValue
	local obamaValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
		obamaValue = gearStore:GetAsync("Player_"..player.UserId)
	end)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local ownsGear = Instance.new("Folder")
	ownsGear.Name = "OwnsGear"
	ownsGear.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = money_name
	money.Parent = leaderstats
	
	local ownsObamaFigure = Instance.new("BoolValue")
	ownsObamaFigure.Name = "OwnsObamaFigure"
	ownsObamaFigure.Parent = ownsGear
	
	if success then
		money.Value = moneyValue
		ownsObamaFigure.Value = obamaValue
	else
		print("Failed to load data")
	end
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
		gearStore:SetAsync("Player_"..player.UsedId, player.OwnsGear.OwnsObamaFigure.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to save data")
	end
end

local function autosave()
	while wait(autosave_cooldown) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	save(player)
end)

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

remote2.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value -= amount
end)

spawn(autosave)
1 Like

Maybe try saving both of the values together in one datastore.

--Saving Data
local success, err = pcall(function()
DataStore:SetAsync("Player"..player.UserId, {player.leaderstats.Money.Value, player.OwnsGear.OwnsObamaFigure.Value})
end)

--Loading Data
local success, err = pcall(function()
data = DataStore:GetAsync("Player"..player.UserId)
end)

if success then
money.Value = data[1]
ownsObamaFigure.Value = data[2]
end

That should work

Have you checked if the API in your game is active?

Also a typo
image

Nothing changed when I used this script

Thanks for pointing out the typo, however even after I change it the game dosen’t save OwnsObamaFigure’s value

You should save a table instead of creating 2 DataStores. Also, instead of creating a loop which will cause a bunch of saving requests, use :BindToClose() to secure data save for the last player who leaves, since this fires before a server shuts down.

-- SETTINGS --
local money_name = "Money"
--------------

local PLS = game:GetService("Players")
local DS = game:GetService("DataStoreService")
local DataSave = DS:GetDataStore("DataSave")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
local remote2 = game:GetService("ReplicatedStorage").Remotes.RemoveCurrency

local function save(player)
	local Data = {
		["Money"] = player.leaderstats.Money.Value;
		["ObamaValue"] = player.OwnsGear.OwnsObamaFigure.Value
	}

	pcall(function()
		DataSave:SetAsync(player.UserId, Data)
	end)
end

PLS.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local ownsGear = Instance.new("Folder")
	ownsGear.Name = "OwnsGear"
	ownsGear.Parent = player

	local money = Instance.new("IntValue")
	money.Name = money_name
	money.Parent = leaderstats

	local ownsObamaFigure = Instance.new("BoolValue")
	ownsObamaFigure.Name = "OwnsObamaFigure"
	ownsObamaFigure.Parent = ownsGear
	
	local Data
	
	local success, err = pcall(function()
		Data = DataSave:GetAsync(player.UserId)
	end)


	if success then
		money.Value = Data.Money or 0
		ownsObamaFigure.Value = Data.ObamaValue or false
	end
end)

game.Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for _, player in ipairs(PLS:GetPlayers()) do
		save(player)
	end
end)

--etc
1 Like

There are no errors and the script prints out that the data is being saved, but only the IntValue is being saved again.