Data Saving not working

  1. What do you want to achieve? Saving player backpack.

  2. What is the issue? It is not working. The tools aren’t saving.

  3. What solutions have you tried so far? I looked up Data Store in the Dev API. It didn’t help.

Here is the script:

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("BackPaccSavee")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = datastore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolFound = game.ReplicatedStorage.MachinesTool:FindFirstChild(v)
				if toolFound then
					toolFound:Clone().Parent = player.Backpack
					toolFound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolSave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolSave,tool.Name)
			end
		end
		datastore:SetAsync("User-"..player.UserId,toolSave)
	end)
end)

All help is appreciated! :smiley:

2 Likes

Hey there, is API services enabled? :thinking:

1 Like

Yep! It is enabled. I enabled all the security features. XD

1 Like

Try adding a wait() before saving. Like this…

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("BackPaccSavee")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = datastore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolFound = game.ReplicatedStorage.MachinesTool:FindFirstChild(v)
				if toolFound then
					toolFound:Clone().Parent = player.Backpack
					toolFound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolSave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolSave,tool.Name)
			end
		end

        wait()
		datastore:SetAsync("User-"..player.UserId,toolSave)
	end)
end)
1 Like

I will test it out now. Thanks! I will let you know if it works or not immediately.

1 Like

Try this in a real game. Maybe that will work.

1 Like

The game is private. But leaderstats saving did work.

1 Like

Although it is private, only you can access to it, so you should still be able to play it, just without other people playing it.

1 Like

Mhm! All I was saying is that the leadertats saving worked, but the tool saving didn’t. Sorry for the confusion! :smiley:

1 Like

Have you tried printing out the error code?

1 Like

Oof. It didn’t work. There were no errors found.

1 Like

Try enabling API in studios, play test the game. Also, I think you used the pcall functions incorrectly, you shouldn’t just wrap the entire logic in it, instead only wrap the part where it saves and loads the data.

This also explains why the error code isn’t printing.

What do you mean by logic? Sorry for the dumb question, I am a beginner.

In simple words, just use the pcall function on the SetAsync and GetAsync function only, it returns two variables, first variable indicating if the function wrapped inside errors, second variable returns the error message if it errors, otherwise it returns whatever that’s returned from the function. You should probably just search YT Videos on how to use DataStoreService.

Actually, try debugging like this…

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("BackPaccSavee")

game.Players.PlayerAdded:Connect(function(player)
	local success, response = pcall(function()
		local tool = datastore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolFound = game.ReplicatedStorage.MachinesTool:FindFirstChild(v)
				if toolFound then
					toolFound:Clone().Parent = player.Backpack
					toolFound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)

    if success then
        print("Successfully loaded ".. player.Name .."'s data")
    else
       print("Failed to load ".. player.Name .."'s data")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, response = pcall(function()
		local toolSave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolSave,tool.Name)
			end
		end

        wait()
		datastore:SetAsync("User-"..player.UserId,toolSave)
	end)

    if success then
        print("Successfully saved ".. player.Name .."'s data")
    else
       print("Failed to save ".. player.Name .."'s data")
    end
end)


No errors, no prints. Don’t mind the errors above, those are from a different script.

Somehow the smooth camera plugin displays the wrong date. XD

Wait. I just saw it. It did load my data, but didn’t save my data.

Which error is the saving data one?