I need help with a datastore

Hello Devforum Members,

Recently I have been looking at a game I made a while back and gave up on. But now that I’m looking at the game I have been testing it for any errors so that I can fix them and continue developing on it.

But I keep getting one error with no errors in the output. The error is, with datastore once the player has left the game it should save what’s in their backpack. The script doesn’t work and the output isn’t showing any errors

The Script:

local datastoreservice = game:GetService("DataStoreService")
local dataStore = datastoreservice:GetDataStore("BackpackSave")

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.Tools: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)

Also if it helps I was following this tutorial (Go to 5:41 minutes to see when they begin the backpack tool saving)

One last thing, I have another datastore saving data but it is not overflowing the datastore queue with datastore’s because there is no error. Could this potentially be the problem?

All help is appreciated

1 Like

Try removing the pcall from it, that should show you the errors.

2 Likes

Should I remove them both?

Also sorry if this is a basic thing to know I am trash at using datastore’s

1 Like

The pcalls prevent errors from showing up, it’s best to remove them both so we know what’s going on

4 Likes

Yes, incase the PlayerAdded function is the issue.

1 Like

After removing the Pcall function, the datastore still didn’t work with still no errors

2 Likes

Show the code how it is right now

edit: It could be because it’s in a function, try getting of the function lines as well

That’s weird. How about you remove the if tool then lines?

local datastoreservice = game:GetService("DataStoreService")
local dataStore = datastoreservice:GetDataStore("BackpackSave")

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

game.Players.PlayerRemoving:Connect(function(player)
		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)

Try doing

local tool = dataStore:GetAsync("User_"..player.UserId)
print(tool) --Put this print
if tool then 

To see if tool is nil or not

Something’s wrong. The last end) is on an indent, but the :Connect lines aren’t indented.

I think it was likely that @pinchpotmonster pasted the code incorrectly and that’s why it looks indented

1 Like

Thank you so much for the help. It has taken me ages to try and figure it out on my own, but I had to ask the devforum. But anyway Thank You! :smiley:

Glad to be of help even though all I mentioned was for you to print it, you sitll need to figure out how to not make it nil, which if I remember correctly, utilises JSONEncode and JSONDecode from the HTTPService if I’m not mistaken. This should work if your data is a table, which looks like it is

1 Like