Adding New Items To DataStore Results In The New Items Defaulting to False or 0 Instead of set default value

Hi, guys!

This is my new datastore script I’ve been using the past 5 months or so. It works quite well. However, there’s a bit of a problem.Any time I add new data to it, and a Player has an already existing datastore, it’ll default to 0, or false, rather than the set default value. I think it has something to do with my cache. This also occurs for new players too for some reason. I was wondering if there was a way to solve this as it’s a bit of a problem I’ve noticed recently that prevents me from adding new features to the game currently. Any help would be greatly appreciated.
Thank you! :slight_smile:


local DataStoreService = game:GetService("DataStoreService")
local SystemDataStore = DataStoreService:GetDataStore("SYSTEM","global")
local currencyTypes = {}
local HttpService = game:GetService("HttpService")
local decoder = require(game.ServerScriptService.UnicornEngineDatastoreService.Resources.DatastoreTableDecoder)


--Create the user's data. string DATANAME, ANYVALUE defaultValue
local createData = function(player)
	
	local data = {
--<<------------------------------------------------------------------------------------------------>>--
"premiumMultiplier","NumberValue",										1;
"isBanned","BoolValue",													false;
"numBans","NumberValue",												0;
"numWarnings","NumberValue",											0,
"storeDiscount","NumberValue",											1,
--<<------------------------------------------------------------------------------------------------>>--
"MatchesWon","NumberValue",												0,
"HighestSpree","NumberValue",											0,
"TotalKills","NumberValue",												0,
"TotalDeaths","NumberValue",											0,
"KDR","NumberValue",													0,
"TotalHeadshots","NumberValue",											0,
"TotalCaptures","NumberValue",											0,
"TotalMatchesWon","NumberValue",										0,
"TotalTimePlayed","NumberValue",										0,
"ChallengesCompleted","NumberValue",									0,
"TotalCreditsEarned","NumberValue",										0,
"FirstTimePlaying","BoolValue",											true,
"BanReason","StringValue",												"",
"XP","NumberValue",														0,
"Rank","NumberValue",													0,
"TotalXPEarnedV2","NumberValue",										0,
"RequiredXP","NumberValue",												7300,
"CreditMultiplier","NumberValue",										1,
"XPMultiplier","NumberValue",											1,
}
	
	
	local Folder = Instance.new("Folder")
	Folder.Parent = player
	Folder.Name = "SYSTEM"



for x=1,#data,3 do
local save;delay(3,function()save=true;end)
local val=Instance.new(data[x+1])
val.Name=data[x]
val.Parent = Folder
end

	
end
--Load data
local loadData = function(player)
	
	local dataLoaded = Instance.new("BoolValue")
	dataLoaded.Value = false
	dataLoaded.Parent = player
	dataLoaded.Name = "DataLoaded"

	LoadedSystemDataStore = SystemDataStore:GetAsync("SYSTEM"..player.UserId)	
	local cache = decoder.convertr(LoadedSystemDataStore)
	cache.Parent = player
	cache.Name = "SYSTEM_Cache"
	
	for _, sysFind in pairs(player.SYSTEM:GetChildren()) do
		if cache:FindFirstChild(sysFind.Name) then
			sysFind.Value = cache[sysFind.Name].Value
			dataLoaded.Value = true
		end
	end
	

	
	game.Debris:AddItem(cache,60)

end


--// Save Data
local saveData = function(player)
if player.DataLoaded.Value == true then
	

	for _, currency in pairs(player.SYSTEM:GetChildren()) do
		currencyTypes[currency.Name] = currency.Value
	end
	SystemDataStore:SetAsync("SYSTEM"..player.UserId, currencyTypes)
	end		
end

game.Players.PlayerAdded:Connect(function(player)

createData(player)
loadData(player)

if player.SYSTEM.TotalDeaths.Value < 0 then
	player.SYSTEM.TotalDeaths.Value = 0
end




end)


local Players = game:GetService("Players")
--// Auto Save
while wait(60) do
	for _, plr in pairs(Players:GetPlayers()) do
		if plr.Parent == Players then
			saveData(plr)
		end
	end
end



I didn’t read through enough to give you a definite answer but it doesn’t seem that you ever use the default values in your createData function.

2 Likes

I am honestly surprised that I didn’t catch that.

Thank you for pointing that out to me! :joy:

Edit:
This definitely fixed my issue. I feel kinda dumb to be honest. Lol
image

1 Like