Tables not saving to data store

Hello!

Somehow, the code reaches the end and states the data is saved. Sadly, when rejoining the game, nothing is saved. I’ve tried figuring out why, but I haven’t succeeded.

I tried putting everything into a single data store since I read it was a better option (rather than having like 5 individual).

Update: Just realized I was printing out the wrong variable. The datastore isn’t saving at all but the table sent is correct.

-- Once player joins game, leaderboard is set and their data is loaded
game.Players.PlayerAdded:Connect(function(player)
	
	-- Data
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Parent = folder
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = folder

	local playerData
	local playerUserId = "Player_"..player.UserId
	-- Loading data stores
	local success, errorMssg = pcall(function()
		playerData = playerDataStore:GetAsync(playerUserId)
	end)

	if success then 
		if playerData then
			
			print(playerData)
			-- Sets all data 
			local folder_leaderstas = playerData["leaderstats"]
			
			for i, data_folder in pairs( playerData ) do
				local tableName = i				
				-- i = leaderstats, owned items, values data
				
				for j, data in pairs(data_folder) do
					--print(j)
					-- j = SODA, etc.
					
					if i == "ownedItems" then
						ownedItems_data:FindFirstChild(j).Value = data
					end
					if i == "leaderstats" then
						folder:FindFirstChild(j).Value = data
					end
					if i == "valuesData" then
						values_data:FindFirstChild(j).Value = data
					end 
					
				end
				
			end
		else
			print("failed")
		
		end
		
		print("Player data should be loaded!")
	else
		warn(errorMssg)
	end
	
	-- Teleport player back to their checkpoint
	if player.Character then
		teleportPlr(player)
	end
	player.CharacterAdded:Connect(function()
		teleportPlr(player)
	end)

end)



local function create_table(player)
		
	-- creates MAIN TABLE for data
	local playerData = {
		[player.leaderstats.Name] = {},
		[values_data.Name] = {},
		[ownedItems_data.Name] = {} 
	}
	
	
	-- get leaderstats data
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		playerData[player.leaderstats.Name][stat.Name] = stat.Value
	end
	
	-- get values data
	for i, value in pairs(values_data:GetChildren()) do
		playerData[values_data.Name][value.Name] = value.Value
	end
	
	-- get shop values data
	for i, item in pairs(shopValues:GetChildren()) do
		playerData[ownedItems_data.Name][item.Name] = item.Value
	end		

	return playerData
	
end



local function saveData(player)
	-- added this bc of a solved devforum post using this.. not sure if it even 
	-- makes a difference
	local localPDS = dataStoreService:GetDataStore("PlayerDataStore", player)

	local playerData = create_table(player)

	-- save player data table
	local success, errorMssg = pcall(function()
		local playerUserId = "Player_"..player.UserId
		localPDS:SetAsync(playerData, playerUserId)
		print("end.. saved data")
		wait(7) 

	end)

	if success then
		-- try to keep saving data here 
		-- try and catch?? add later
		print("Player data saved!")
		print(playerData)
	else
		warn(errorMssg)
	end
end



--Once player leaves, their data is saved
game.Players.PlayerRemoving:Connect(function(player)	
	saveData(player)
	
end)


-- i read this gives more security when saving -> runs when server shuts down 
local RunService = game:GetService("RunService")

game:BindToClose(function() -- (only needed inside Studio)
	for i, player in pairs( game.Players:GetPlayers() ) do
		wait()
		saveData(player)
		if RunService:IsStudio() then -- runs when using roblox studio
			wait(3) 
		end
	end
end)




What are you doing here? Why are you getting the DataStore with the player set as a scope, and saving it an additional time to the players user id here:

local playerUserId = "Player_"..player.UserId
localPDS:SetAsync(playerData, playerUserId)

You should instead just define it once at the top of the script, and not define it every time inside of a function. It is possible you have mixed up the DataStores and saving to/loading from the wrong one.

2 Likes

You never set the values of Stage and Rebirths?

Thank you! I removed:

and had my parameters switched as you said:

playerDataStore:SetAsync(playerUserId, playerData)

It works now

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