Issue with data saving

Hello, I am having some data issue while saving, I don’t know why but when I first play the game It doesn’t save. Second time I get in game then it start’s saving cars. I don’t see any problems. I hope some people here can help me.

local DATASTORE_KEY = "DealershipData"
local ServerStorage = game:GetService("ServerStorage")

local function saveModelToDataStore(player)
	local dataToSave = {}
	local SaveFolder = ServerStorage:WaitForChild("PurchasedVehicles"):FindFirstChild(player.UserId)

	if SaveFolder then

		for _, car in ipairs(ServerStorage:WaitForChild("PurchasedVehicles"):FindFirstChild(player.UserId):GetChildren()) do
			local objData = {}
			objData.Name = car.Name

			dataToSave[car.Name] = objData
		end

		local success, errorMessage = pcall(function()
			game:GetService("DataStoreService"):GetDataStore(DATASTORE_KEY):SetAsync(tostring(player.UserId), dataToSave)
			print("Saved data for player:", player.Name)
		end)

		if not success then
			warn("Failed to save data for player:", player.Name, errorMessage)
		end
	else
		warn("Player folder not found")
	end
end

local function loadModelFromDataStore(player)
	local savedData = game:GetService("DataStoreService"):GetDataStore(DATASTORE_KEY):GetAsync(tostring(player.UserId))

	if savedData then
		for name, objData in pairs(savedData) do
			local vehicle = ServerStorage:WaitForChild("Vehicles"):FindFirstChild(name)
			if vehicle then
				local CloneVeh = vehicle:Clone()
				CloneVeh.Parent = ServerStorage:WaitForChild("PurchasedVehicles"):FindFirstChild(player.UserId)
				print("Loaded vehicle: "..CloneVeh.Name)
			end
		end
	else
		print("No saved data found for player:", player.Name)
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	saveModelToDataStore(player)
end)

game.Players.PlayerAdded:Connect(function(player)
	local PlayerPurchasedVehiclesFolder = Instance.new("Folder",ServerStorage.PurchasedVehicles)
	PlayerPurchasedVehiclesFolder.Name = player.UserId

	wait(1)
	loadModelFromDataStore(player)
end)

A lot of this could take a while to save and initialize. Although it works the second time, try adding some print statements to see if it gets past all this stuff the first time

And why not define the DataStore in the global env instead? You won’t have to have the DATASTORE_KEY constant and you don’t have to define it twice

I put prints to see if it’s called and first time in game it doesn’t get called and also defined data global env.

1 Like

Have you already checked if “SaveFolder” exists the first time around?

@ MasterDiagnose

local DATASTORE_KEY = "DealershipData"
local ServerStorage = game:GetService("ServerStorage")

local function saveModelToDataStore(player)
	local dataToSave = {}
	local SaveFolder = ServerStorage.PurchasedVehicles:FindFirstChild(player.UserId)

	if SaveFolder then
		for _, car in ipairs(SaveFolder:GetChildren()) do
			dataToSave[car.Name] = true
		end

		pcall(function()
			game:GetService("DataStoreService"):GetDataStore(DATASTORE_KEY):SetAsync(tostring(player.UserId), dataToSave)
		end)
	end
end

local function loadModelFromDataStore(player)
	local savedData = game:GetService("DataStoreService"):GetDataStore(DATASTORE_KEY):GetAsync(tostring(player.UserId))

	if savedData then
		for name, _ in pairs(savedData) do
			local vehicle = ServerStorage.Vehicles:FindFirstChild(name)
			if vehicle then
				vehicle:Clone().Parent = ServerStorage.PurchasedVehicles:FindFirstChild(player.UserId)
			end
		end
	end
end

game.Players.PlayerRemoving:Connect(saveModelToDataStore)

game.Players.PlayerAdded:Connect(function(player)
	local PlayerPurchasedVehiclesFolder = Instance.new("Folder", ServerStorage.PurchasedVehicles)
	PlayerPurchasedVehiclesFolder.Name = player.UserId
	loadModelFromDataStore(player)
end)

pretty close to what you had … just a few tweaks
untested

1 Like

Pretty good. I was actually gonna recommend that too lol

Ah wow that’s an easy solution

1 Like

This works now, it saved first time I tried.

1 Like

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