Why is my saving data system not succeeding or failing?

Why is my saving system not working? And it is not sending the error or succeed print message in the save data script. How do I fix this?

Saving data server script:

--saving data script
local DatastoreService = game:GetService("DataStoreService")
local Datastore = DatastoreService:GetDataStore("Data_2SDyBd851R")

local HttpService = game:GetService("HttpService")
local result = HttpService:GenerateGUID(true)
 --> Example output: {04AEBFEA-87FC-480F-A98B-E5E221007A90}

-- Server script
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	local Pets = Instance.new("Folder", player)
	local Cash = Instance.new("NumberValue", leaderstats)
	local DataObject = {0, {}} -- Cash, Pets
	
	leaderstats.Name = "leaderstats"
	Pets.Name = "Pets"
	Cash.Name = "Cash"
	Cash.Value = 9999999
	leaderstats.Name = "leaderstats"
	
	local allow = Instance.new("StringValue")
	allow.Name = "allow" -- change to what ever you want for the name of the currency.
	allow.Parent = player
	leaderstats.Parent = player
	allow.Value = "false"
	
	--load data
	local success,fail = pcall(function()
		DataObject = Datastore:GetAsync(player.UserId.. "Data")
	end)
	if success then
		if DataObject then
			print("data detected")
			--// Cash
			if DataObject[1] then
				Cash.Value = DataObject[1]
			end
			--// Pets
			if DataObject[2] then
				
				--for Pet,Amount in pairs (DataObject[2]) do
			--		local NumberValue = Instance.new("NumberValue", Pets)
				--		NumberValue.Name = Pet
			--		print(Amount)
			--		NumberValue.Value = Amount
			--	end
				
				for Pet,Amount in pairs (DataObject[2]) do
					if not Pets:FindFirstChild(Pet) then
				local NumberValue = Instance.new("NumberValue", Pets)
				NumberValue.Name = Pet
				NumberValue.Value = Amount
				end
				end
			end
		end
	else
		warn("Error loading data: " ..fail)
	end
end)

Where is the part where it actually saves the data?

Hello!
Your save system is pretty wrong and you don’t saving data you are loading data that don’t exist.

Change your script to this:

--//Services
local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")

--//Variables
local PlayerData = DatastoreService:GetDataStore("Data_2SDyBd851R")

--//Services
local function SaveData(player, data)
	local success, errorMessage = pcall(function()
		PlayerData:SetAsync(player.UserId, data)
	end)
	
	if not success then
		warn(errorMessage)
	end
end

Players.PlayerAdded:Connect(function(player)
	local DataObject = PlayerData:GetAsync(player.UserId.. "Data") or {0, {}}

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("NumberValue")
	Cash.Name = "Cash"
	Cash.Value = DataObject[1]
	Cash.Parent = leaderstats

	local allow = Instance.new("StringValue")
	allow.Name = "allow"
	allow.Value = false
	allow.Parent = player
	
	local Pets = Instance.new("Folder")
	Pets.Name = "Pets"
	Pets.Parent = player
	
	for Pet, Amount in pairs(DataObject[2]) do
		if not Pets:FindFirstChild(Pet) then
			local NumberValue = Instance.new("NumberValue")
			NumberValue.Name = Pet
			NumberValue.Value = Amount
			NumberValue.Parent = Pets
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local data = {
		[1] = player.leaderstats.Cash.Value,
		[2] = {}
	}
	
	for i, pet in ipairs(player.Pets:GetChildren()) do
		table.insert(data[2], pet.Name)
	end
	
	SaveData(player, data)
end)

game:BindToClose(function()
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			local data = {
				[1] = player.leaderstats.Cash.Value,
				[2] = {}
			}

			for i, pet in ipairs(player.Pets:GetChildren()) do
				table.insert(data[2], pet.Name)
			end

			SaveData(player, data)
		end)
	end
end)

You need to add a data saving function.

1 Like