Data Stores Not Working

  1. What do you want to achieve? Use Data Store

  2. What is the issue? this thing just started happening recently and I don’t know what caused it or how did it happen

  3. What solutions have you tried so far? tried looking on DevForum, but still no solution, I also created a new game and the data store not working as well, so the cause of the problem is either my script or the roblox studio itself

note: My data stores work before this thing happened, it just started happening recently. and yeah i tried to test if it works in roblox player, but same result. (No errors as well)

Data Store Script

local dataStore = game:GetService("DataStoreService")
local playerStatsData = dataStore:GetDataStore("TestStat1")

game.Players.PlayerAdded:Connect(function(player)
	local userId = player.UserId
	
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = "PlayerData"
	dataFolder.Parent = player
	
	local playerStats = Instance.new("Folder")
	playerStats.Name = "PlayerStats"
	playerStats.Parent = dataFolder
	
	-- Stats ==
	
	local maxHealth = Instance.new("IntValue")
	maxHealth.Name = "MaxHealth"
	maxHealth.Parent = playerStats
	
	local health = Instance.new("IntValue")
	health.Name = "Health"
	health.Parent = playerStats
	
	local maxStamina = Instance.new("IntValue")
	maxStamina.Name = "MaxStamina"
	maxStamina.Parent = playerStats
	
	local stamina = Instance.new("IntValue")
	stamina.Name = "Stamina"
	stamina.Parent = playerStats
	
	local maxOxygen = Instance.new("IntValue")
	maxOxygen.Name = "MaxOxygen"
	maxOxygen.Parent = playerStats
	
	local oxygen = Instance.new("IntValue")
	oxygen.Name = "Oxygen"
	oxygen.Parent = playerStats
	
	local speed = Instance.new("IntValue")
	speed.Name = "Speed"
	speed.Parent = playerStats
	
	-- Load Data ---------------------------------------------->
	
	local data 
	local success, err = pcall(function()
		data = playerStatsData:GetAsync(userId .. "_Stats")
	end)
	
	if data then
		for i, item in pairs(playerStats:GetChildren()) do
			item.Value = data[item.Name]
		end
	else
		maxHealth.Value = 100
		health.Value = 100
		maxOxygen.Value = 100
		oxygen.Value = 100
		maxStamina.Value = 60
		stamina.Value = 60
	end
end)

local function createDataTable(player)
	local dataFolder = player:WaitForChild("PlayerData")
	local playerStats = dataFolder:WaitForChild("PlayerStats")
	
	local statsTable = {}
	
	for i, item in pairs(playerStats:GetChildren()) do
		statsTable[item.Name] = item.Value
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	local statsTable = createDataTable(player)
	local userId = player.UserId
	
	local success, err = pcall(function()
		playerStatsData:UpdateAsync(userId .. "_Stats", function(recentData)
			local dataTable = recentData or {}
			
			for item, value in pairs(dataTable) do
				dataTable[item] = value
			end
		end)
	end)
	
	if err then
		print(player.Name .. "'s Stats failed to save, cause: ".. err)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Experience shutting down")
		end
	end
	
	wait(5)
end)

i dont really know whats going on.

Replace if data then with if success then. Hope this worked.

Either do what this person suggested (use the success boolean) or use if #data then …

If data will error as it is not a Boolean, and cannot be read as a true or false variable, using # will count the arrays within a table (you’d need to make your data a table first)

guys i… fixed it- and the solution was

game.Players.PlayerRemoving:Connect(function(player)
	local statsTable = createDataTable(player)
	local userId = player.UserId
	
	local playerKey = userId .. "_stats"
	
	local success, err = pcall(function()
		playerStatsData:UpdateAsync(playerKey, function(recentData)
			local dataTable = recentData or {}

			for item, value in pairs(statsTable) do
				dataTable[item] = value
			end			
			
			return dataTable -- THIS SIMPLE LINEEEE DRE WHAT YOU DOINNNN
		end)
	end)
	
	if err then
		print(player.Name .. "'s Stats failed to save, cause: ".. err)
	end
end)

thanks for the replies though, have a good day yall