DataStoreService Saving Randomly

  1. I am trying to make a data store that saves 3 different folders of values and their values.

  2. The issue is that this data store is saving randomly. It sometimes save and sometimes doesn’t save at all.

3.I’ve tried look on the DevForum and the DevHub and have not found any answers so far.

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local playerData = DataStoreService:GetDataStore("PlayerData")

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


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

	local codes = Instance.new("Folder")
	codes.Name = "Codes"
	codes.Parent = player

	local code1 = Instance.new("BoolValue")
	code1.Name = "RELEASE"
	code1.Parent = codes

	local code2 = Instance.new("BoolValue")
	code2.Name = "SPIKEOUTGAMES"
	code2.Parent = codes

	local code3 = Instance.new("BoolValue")
	code3.Name = "REMIEL430"
	code3.Parent = codes

	local code4 = Instance.new("BoolValue")
	code4.Name = "CLZIE"
	code4.Parent = codes

	local code5 = Instance.new("BoolValue")
	code5.Name = "6BCG"
	code5.Parent = codes

	local code6 = Instance.new("BoolValue")
	code6.Name = "PIXLEBIRD"
	code6.Parent = codes

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

	local toys = Instance.new("IntValue")
	toys.Name = "Toys"
	toys.Parent = leaderstats

	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirth"
	rebirth.Parent = leaderstats

	local goldenToys = Instance.new("IntValue")
	goldenToys.Name = "Golden Toys"
	goldenToys.Parent = leaderstats

	local rebirthInfo = Instance.new("Folder")
	rebirthInfo.Parent = player
	rebirthInfo.Name = "RebirthInfo"

	local requiredMoney = Instance.new("IntValue")
	requiredMoney.Parent = rebirthInfo
	requiredMoney.Name = "RequiredMoney"
	requiredMoney.Value = 5000

	local mutiplyerReward = Instance.new("IntValue")
	mutiplyerReward.Parent = rebirthInfo
	mutiplyerReward.Name = "MutiplyerReward"
	mutiplyerReward.Value = 2

	local goldToyReward = Instance.new("IntValue")
	goldToyReward.Parent = rebirthInfo
	goldToyReward.Name = "GoldToyReward"
	goldToyReward.Value = 1

	local presentReward = Instance.new("IntValue")
	presentReward.Parent = rebirthInfo
	presentReward.Name = "PresentReward"

	local Debounce = Instance.new("BoolValue")
	Debounce.Value = false
	Debounce.Name = "Debounce"
	Debounce.Parent = player
	
	local playerUserId = "Player_"..player.UserId
	local data = playerData:GetAsync(playerUserId)
	if data then
		print("Data Found!")
		code1.Value = data["RELEASE"]
		code2.Value = data["SPIKEOUTGAMES"]
		code3.Value = data["REMIEL430"]
		code4.Value = data["CLZIE"]
		code5.Value = data["6BCG"]
		code6.Value = data["PIXLEBIRD"]
		money.Value = data["Money"]
		toys.Value = data["Toys"]
		rebirth.Value = data["Rebirth"]
		goldenToys.Value = data["GoldenToys"]
		requiredMoney.Value = data["RequiredMoney"]
		mutiplyerReward.Value = data["MutiplyerReward"]
		goldToyReward.Value = data["GoldToyReward"]
		presentReward.Value = data["PresentReward"]
	end
end)

local function createStatsTable(player)
	local player_stats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	for i, codes in pairs(player.Codes:GetChildren()) do
		player_stats[codes.Name] = codes.Value
	end
	for i, rebirth in pairs(player.leaderstats:GetChildren()) do
		player_stats[rebirth.Name] = rebirth.Value
	end
	return player_stats    
end

game.Players.PlayerRemoving:Connect(function(player)
	local player_stats = createStatsTable(player)
	local sucess, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		playerData:SetAsync(playerUserId, player_stats)
	end)

	if not sucess then
		warn("Could Not Save Data!")
	end
end)

game:BindToClose(function(player)
	if RunService:IsStudio() then
		return
	end

	print("Saving Data...")

	local player_stats = createStatsTable(player)
	local sucess, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		playerData:SetAsync(playerUserId, player_stats)
	end)

	if not sucess then
		warn("Could Not Save Data!")
	end
end)```

DataStoreService often doesn’t work well in Studio, so it sometimes works and sometimes doesn’t. Have you tried testing it in the actual game?

1 Like

Thank You! I tested it in an actual game and is working perfect!

1 Like