Help with Saving Values in a Folder to Datastore

I am currently making a game that will save data when a player leaves and load data as soon as the player joins. To do the loading and saving, I am making a Datastore with multiple values, so I chose to use a dictionary to save all the data into one table. I am also using a folder object with value objects inside (in ServerStorage), so I can clone it and parent the clone to the player. Loading and saving the data is perfectly fine, however whenever I try to save the data, it saves the default values that are set to the value objects, and not the values set inside the player.

The most I’ve done is swap ipairs and pairs to get any type of different result, but it remains the same. I also consulted the Roblox Creator Documentation, primarily the Tables page, but I have no idea how it saves the default values. What I do know from debugging is that the load function works as intended, so it probably doesn’t have to do with the problem.

Save function in my Datastore script:

local function saveData(player)
	print("Saving data")
	local saveTable = {}
	for _, object in pairs(player.dataFolder:GetDescendants()) do
		if object:IsA("IntValue") or object:IsA("StringValue") or object:IsA("BoolValue") then
			saveTable[object.Name] = object.Value
            -- Line above could be issue, but I'm not sure how to fix
		end
	end
	print("Created saveTable for "..player.UserId)
	local success, errorMsg = pcall(function()
		playerDataStore:SetAsync(player.UserId, saveTable)
	end)
	if success then
		if serverShutDown == false then
			print("Server has not shut down yet")
		else
			playersSavedList[playersSavedCount] = player.UserId
			playersSavedCount += 1
		end
		print(player.UserId.." was successfully saved")
		print(playerDataStore:GetAsync(player.UserId))
	else
		warn(player.UserId.." was not successfully saved")
		warn(errorMsg)
	end
end

game:GetService("Players").PlayerRemoving:Connect(saveData)

My log:

-- When I start the game
  16:58:58.876  Loading data  -  Server - playerDataStore:8
  16:59:00.215  No existing data or error in loading for 206110467, keeping default data  -  Server - playerDataStore:14
-- When I close the game (after setting my folder's money value to 1000):
  16:59:14.789  Saving data  -  Server - playerDataStore:27
  16:59:14.790  Created saveTable for 206110467  -  Server - playerDataStore:34
  16:59:14.790  Disconnect from ::ffff:127.0.0.1|50272  -  Studio
  16:59:15.128  206110467 was successfully saved  -  Server
  16:59:15.129   ▶ {...} Instance  -  Server
  16:59:15.796  Server has shut down  -  Server
-- {...} Instance returns:
{
    ["containerType"] = "None",
    ["cookieType1"] = "None",
    ["cookieType2"] = "None",
    ["cookieType3"] = "None",
    ["money"] = 0,
    ["ovenType"] = "None"
}

My player data folder that gets inserted in the player once PlayerAdded:
playerDataFolder

Hope I gave enough information. Anything that points me to the right direction to solving this is greatly appreciated!

How are you updating the player’s ValueObjects? Are you updating them from the server – by Script or in a test server? If the values are still seen as the default values on the server, you may be updating the values locally instead of from the server.

Thank you for the reply! I didn’t even think twice the problem would be in the testing rather than the actual script! The script now saves the player’s data rather than the default values. Thanks again!

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