Datastore not properly saving certain values?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? figure out what is wrong with my datastore

  2. What is the issue? my datastore saves most values, but it does not save Booleans that are set to true originally, and I have no idea how this is possible or why it is happening

  3. What solutions have you tried so far? renaming the booleans, changing their parent from folder to player, printing the saved values and loaded values

basically the booleans inside of the player that are set to true originally in the values creation script do NOT get saved by the datastore script and get set back to true when changed

my datastore script:

local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")
local loaded = {}

local Event = game.ReplicatedStorage:WaitForChild("Daily")

local Rewards = {
	[1] = function(player: Player)
		player.leaderstats.Cash.Value += 300
		Event:FireClient(player, "You got 300 cash come back tommorow for more",1)
	end,
	[2] = function(player: Player)
		player.leaderstats.Cash.Value += 600
		Event:FireClient(player, "You got 600 cash come back tommorow for more",2)
	end,
	[3] = function(player: Player)
		player.leaderstats.Cash.Value += 900
		Event:FireClient(player, "You got 900 cash come back tommorow for more",3)
	end,
	[4] = function(player: Player)
		player.leaderstats.Cash.Value += 1200
		Event:FireClient(player, "You got 1200 cash come back tommorow for more",4)
	end,
	[5] = function(player: Player)
		player.leaderstats.Cash.Value += 1500
		Event:FireClient(player, "You got 1500 cash come back tommorow for more",5)
	end,
	[6] = function(player: Player)
		player.leaderstats.Cash.Value += 1800
		Event:FireClient(player, "You got 1800 cash come back tommorow for more",6)
	end,
	[7] = function(player: Player)
		player.leaderstats.Cash.Value += 2100
		Event:FireClient(player, "You got 2100 cash, streak set back to 1 enjoy tho",7)
	end,
}

local function getServerType()
	if game.PrivateServerId ~= "" then
		if game.PrivateServerOwnerId ~= 0 then
			return "VIPServer"
		else
			return "ReservedServer"
		end
	else
		return "StandardServer"
	end
end


game.Players.PlayerAdded:connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	if player.UserId > 0 and player.Parent then
		local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
		if leaderstatsData ~= "Request rejected" then
			if leaderstatsData then
				for i, stat in ipairs(leaderstats:GetChildren()) do
					local value = leaderstatsData[stat.Name]
					if value then
						stat.Value = value
					end
				end
				for i, stat in ipairs(player:GetChildren()) do
					if stat.Name ~= "Reversed" then
					local value = leaderstatsData[stat.Name]
					if value then
						stat.Value = value
					end
					end
				end
			end
			print(leaderstatsData)
			if leaderstatsData == nil or type(leaderstatsData) ~= "table" or leaderstatsData.Claimed == nil then
				leaderstatsData = {
					Claimed = false,
					LastLogin = 0,
					Streak = 1,
				}
			end

			player:SetAttribute("DailyClaimed", leaderstatsData.Claimed)
			player:SetAttribute("LastLogin", leaderstatsData.LastLogin)
			player:SetAttribute("Streak", leaderstatsData.Streak)
			loaded[player] = true
			
			if getServerType() == "VIPServer" then return end
			if (tonumber(os.date("%j")) - 1) >= tonumber(player:GetAttribute("LastLogin") + 2) then
				player:SetAttribute("Streak", 1)
			end
			if (tonumber(os.date("%j")) - 1) >= tonumber(player:GetAttribute("LastLogin")) then-- and player:GetAttribute("DailyClaimed") == false then
				local Streak = player:GetAttribute("Streak")

				player:SetAttribute("DailyClaimed", true)
				player:SetAttribute("LastLogin", os.date("%j"))

				Rewards[Streak](player)

				player:SetAttribute("Streak", Streak + 1)

				if player:GetAttribute("Streak") > #Rewards then
					player:SetAttribute("Streak", 1)
				end
			end
		end
	end
	if loaded[player] == false then
		player:Kick("there was a problem loading your data, rejoin and your data should be just fine")
	end
end)

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

function save(player)
	if getServerType() == "VIPServer" then return end
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		if loaded[player] then
			local leaderstatsData = {
				Claimed = player:GetAttribute("DailyClaimed") or false,
				LastLogin = player:GetAttribute("LastLogin") or os.date("%j"),
				Streak = player:GetAttribute("Streak") or 1,
			}
			for i, stat in ipairs(leaderstats:GetChildren()) do
				leaderstatsData[stat.Name] = stat.Value
			end
			for i, stat in ipairs(player:GetChildren()) do
				if stat.Name ~= "leaderstats" then
					if stat:IsA("NumberValue") or stat:IsA("StringValue") or stat:IsA("BoolValue") then
						leaderstatsData[stat.Name] = stat.Value
					end
				end
			end
			local cash = player.leaderstats.Cash.Value

			local questsFolder = player.Quests
			local lastRefresh = questsFolder.LastRefresh.Value
			local currentQuests = {}

			for i, child in pairs(questsFolder:GetChildren()) do
				if child:IsA("Folder") then

					local questDesc = child.Name
					local progress = child.Progress.Value
					local completed = child:FindFirstChild("Completed") and true

					currentQuests[questDesc] = {progress, completed}
				end
			end

			leaderstatsData.refresh = lastRefresh
			leaderstatsData.quests = currentQuests
			print(leaderstatsData)
			leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
		end
	end
	loaded[player] = nil
end

game:BindToClose(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		save(v)
	end
end)

local save = {}
game.ReplicatedStorage:WaitForChild("Save").OnServerEvent:Connect(function(plr)
	if table.find(save,plr.Name) then return end
	table.insert(save,plr.Name)
	save(plr)
end)

any help would be appreciated

You can use ProfileService to store booleans easier, and it’s the best way to save player’s data.

1 Like

I will give that a try, thanks!

1 Like

You shouldn’t give solutions to people who didn’t help you at all.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.