DataStore gives back default BoolValue even if its changed

Hello! I’ve been running into the following issue: When joining the game, the “JoinedFirstTime” BoolValue, which is loaded and saved by the datastore, changes to true from false like its supposed to do even after rejoining. But for some reason, when using a script in ServerScrioptService where the BoolValue must be set to true nothing happens, while if I check false instead, it works. So I suppose it still detects the default value even if the BoolValue has changed. Underneath are my scripts:

Datastore

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("UltimateLevelingData")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

if RunService:IsStudio() then
	dataStore = dataStoreService:GetDataStore("TestData")
	print("Loaded Test Database")
else
	print("Loaded Production Database")
end

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local rank = Instance.new("StringValue")
	rank.Name = "Rank"
	rank.Value = "E"
	rank.Parent = leaderstats

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = 1
	level.Parent = leaderstats

	local statistics = Instance.new("Folder")
	statistics.Name = "Stats"
	statistics.Parent = plr

	local strenght = Instance.new("IntValue")
	strenght.Name = "Strenght"
	strenght.Value = 1
	strenght.Parent = statistics
	
	local agility = Instance.new("IntValue")
	agility.Name = "Agility"
	agility.Value = 1
	agility.Parent = statistics
	
	local vitality = Instance.new("IntValue")
	vitality.Name = "Vitality"
	vitality.Value = 1
	vitality.Parent = statistics
	
	local perception = Instance.new("IntValue")
	perception.Name = "Perception"
	perception.Value = 1
	perception.Parent = statistics
	
	local intelligence = Instance.new("IntValue")
	intelligence.Name = "Intelligence"
	intelligence.Value = 1
	intelligence.Parent = statistics
	
	local plrStatsData = Instance.new("Folder")
	plrStatsData.Name = "Data"
	plrStatsData.Parent = plr

	local hp = Instance.new("IntValue")
	hp.Name = "HP"
	hp.Value = 100
	hp.Parent = plrStatsData

	local maxHP = Instance.new("IntValue")
	maxHP.Name = "MaxHP"
	maxHP.Value = 100
	maxHP.Parent = plrStatsData
	
	local mp = Instance.new("IntValue")
	mp.Name = "MP"
	mp.Value = 100
	mp.Parent = plrStatsData
	
	local maxMP = Instance.new("IntValue")
	maxMP.Name = "MaxMP"
	maxMP.Value = 100
	maxMP.Parent = plrStatsData

	local characterCreated = Instance.new("BoolValue")
	characterCreated.Name = "CharacterCreated"
	characterCreated.Value = false
	characterCreated.Parent = plrStatsData

	local joinedFirstTime = Instance.new("BoolValue")
	joinedFirstTime.Name = "JoinedFirstTime"
	joinedFirstTime.Value = false
	joinedFirstTime.Parent = plrStatsData

	local success, errorMessage = pcall(function()
		local savedData = dataStore:GetAsync(plr.UserId .. "_data")
		
		if savedData then
			print(`Successfully loaded data for {plr.Name}({plr.UserId})`)
			rank.Value = savedData.rank
			level.Value = savedData.level
			strenght.Value = savedData.strenght
			agility.Value = savedData.agility
			vitality.Value = savedData.vitality
			perception.Value = savedData.perception
			intelligence.Value = savedData.intelligence
			hp.Value = savedData.hp
			maxHP.Value = savedData.maxhp
			mp.Value = savedData.mp
			maxMP.Value = savedData.maxmp
			characterCreated.Value = savedData.charactercreated
			joinedFirstTime.Value = savedData.joinedfirsttime
		end
	end)
	
	if not success then
		print(`Error when loading data for {plr.Name}({plr.UserId})`)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local dataToSave = {
		rank = plr.leaderstats.Rank.Value,
		level = plr.leaderstats.Level.Value,
		strenght = plr.Stats.Strenght.Value,
		agility = plr.Stats.Agility.Value,
		vitality = plr.Stats.Vitality.Value,
		perception = plr.Stats.Perception.Value,
		intelligence = plr.Stats.Intelligence.Value,
		hp = plr.Data.HP.Value,
		maxhp = plr.Data.MaxHP.Value,
		mp = plr.Data.MP.Value,
		maxmp = plr.Data.MaxMP.Value,
		charactercreated = plr.Data.CharacterCreated.Value,
		joinedfirsttime = plr.Data.JoinedFirstTime.Value
	}
	
	local tries = 3
	while tries > 0 do
		local success, errorMessage = pcall(function()
			dataStore:SetAsync(plr.UserId .. "_data", dataToSave)
		end)

		if success then
			print(`Successfully saved data for {plr.Name}({plr.UserId})`)
			break
		else
			tries -= 1
			print(`Error when saving data for {plr.Name}({plr.UserId})`)
			task.wait(1)
		end
	end
end)

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		if player.leaderstats then
			local dataToSave = {
				rank = player.leaderstats.Rank.Value,
				level = player.leaderstats.Level.Value,
				strenght = player.Stats.Strenght.Value,
				agility = player.Stats.Agility.Value,
				vitality = player.Stats.Vitality.Value,
				perception = player.Stats.Perception.Value,
				intelligence = player.Stats.Intelligence.Value,
				hp = player.Data.HP.Value,
				maxhp = player.Data.MaxHP.Value,
				mp = player.Data.MP.Value,
				maxmp = player.Data.MaxMP.Value,
				charactercreated = player.Data.CharacterCreated.Value,
				joinedfirsttime = player.Data.JoinedFirstTime.Value
			}
			dataStore:SetAsync(player.UserId .. "_data", dataToSave)
		end
	end
end)

while true do
	task.wait(300)
	for _, player in ipairs(Players:GetPlayers()) do
		if player.leaderstats then
			local dataToSave = {
				rank = player.leaderstats.Rank.Value,
				level = player.leaderstats.Level.Value,
				strenght = player.Stats.Strenght.Value,
				agility = player.Stats.Agility.Value,
				vitality = player.Stats.Vitality.Value,
				perception = player.Stats.Perception.Value,
				intelligence = player.Stats.Intelligence.Value,
				hp = player.Data.HP.Value,
				maxhp = player.Data.MaxHP.Value,
				mp = player.Data.MP.Value,
				maxmp = player.Data.MaxMP.Value,
				charactercreated = player.Data.CharacterCreated.Value,
				joinedfirsttime = player.Data.JoinedFirstTime.Value
			}
			dataStore:SetAsync(player.UserId .. "_data", dataToSave)
			print(`Successfully saved data for {player.Name}({player.UserId})`)
		end
	end
end

Script that checks if the BoolValue is set to false:

game.Players.PlayerAdded:Connect(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local JoinedFirstTime = player:WaitForChild("Data"):WaitForChild("JoinedFirstTime")
		local char = player.Character or player.CharacterAdded:Wait()
		local hum = char:WaitForChild("Humanoid")
		local desc = hum:GetAppliedDescription(hum)

		if JoinedFirstTime.Value == false then --Works if false, doesn't work if true even if the BoolValue is set to true
			print("test")
			char.Hair:Destroy()
			char.Face:Destroy()
			desc.HairAccessory = "122928544683214"
			desc.FaceAccessory = "108408358816244"
			hum:ApplyDescription(desc)
			JoinedFirstTime.Value = true
		end
	end
end)

Since they are 2 separate scripts the code checking if the value is false will run the moment BoolValue is available, meaning it will run before your data has loaded. You can either move the code checking if the value is false to the same function where your data is loaded or have some event that fires/value that changes indicating the data has been loaded and only then should you check if the value is false.

Fixed, thanks! Had to actually move the script in the function you told.

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