Tool Datastores?

Trying to make a tool datastore. I tried a devforum tutorial,

local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage.ItemsFolder --replace it to the location of your item folder

game.Players.PlayerAdded:Connect(function(player)
	local inventory = inventorydata:GetAsync(player.UserId) or {}
	for i, si in pairs(inventory) do
		if itemsfolder:FindFirstChild(si) then
			itemsfolder[si]:Clone().Parent = player.Inventory--Change this to the folder you want to copy the items back into.
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inventory = {}
	for i, si in pairs(player:WaitForChild("Inventory"):GetChildren()) do-- Change this to the folder you want to save the children of.
		table.insert(si.Name)
	end
	local success, errormsg = pcall(function()
		inventorydata:SetAsync(player.UserId, inventory)
	end)
	if errormsg then warn(errormsg)
	end
end)

And I used this code to try and test it

game.Players.PlayerAdded:Connect(function(player)
	wait(12)
	player:WaitForChild('Backpack'):WaitForChild('Tool'):Destroy()
end)

It didn’t save. I’ve tried tweaking the script a little but I can’t really seem to fix it.

2 Likes

in the playerremoving function, there was the table.insert() function there, replace that with table.insert(inventory, si.Name)

Try this, I just made a few changes:

local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage:WaitForChild("ItemsFolder")

game.Players.PlayerAdded:Connect(function(player)
	local succ, invent = pcall(function()
		return inventorydata:GetAsync(player.UserId)
	end) 
		
	local newFolder = Instance.new("Folder")
	newFolder.Name = "Inventory"
	newFolder.Parent = player
	
	if succ then
		if invent then
			warn("player has data")
			for i, si in pairs(invent) do
				if itemsfolder:FindFirstChild(si) then
					itemsfolder[si]:Clone().Parent = player.Inventory
				end
			end
		else
			warn("player has NO data")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inventory = {}
	for i, si in pairs(player:WaitForChild("Inventory"):GetChildren()) do
		table.insert(si.Name)
	end
	local success, errormsg = pcall(function()
		inventorydata:SetAsync(player.UserId, inventory)
	end)
	if errormsg then warn(errormsg)
	end
end)

What you mean, you tried to use that code to test it?

I tried testing the save function with by removing the tool 12 seconds after the player joins, After I left I rejoined just to see that i still have the tool

this code here worked for me (with a few edits)

local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage:WaitForChild("ItemsFolder")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local succ, err = pcall(function()
		data = inventorydata:GetAsync(player.UserId)
	end) 

	local newFolder = Instance.new("Folder")
	newFolder.Name = "Inventory"
	newFolder.Parent = player

	if succ then
		if data then
			warn("player has data")
			for i, si in pairs(data) do
				if itemsfolder:FindFirstChild(si) then
					local clone = itemsfolder[si]:Clone()
					clone.Parent = newFolder
				end
			end
		else
			warn("player has NO data")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inventory = {}
	for i, si in pairs(player:FindFirstChild("Inventory"):GetChildren()) do
		table.insert(inventory, si.Name)
	end
	local success, errormsg = pcall(function()
		inventorydata:SetAsync(player.UserId, inventory)
	end)
	if errormsg then warn(errormsg)
	end
end)

You only changed this line in my code, and its doing the same ¬¬
Using return in a pcall or using a variable its the same…

This time i tried something different. I added the tool instead of removing it

game.Players.PlayerAdded:Connect(function(player)
	wait(12)
	local Tool = script.Tool:Clone()
	Tool.Parent = player:WaitForChild('Backpack')
end)

After getting the item and rejoining it says i have data but doesn’t give me back my tool

o the return thing wasn’t working for me for some reason, ik thats a way to do it but i tried it the other way and it was prob returning a blank table when he joined cause he used table.insert(si.Name) when the documentation for table.insert says table.insert(array, thing to insert)

When player joins player is not given with a Tool in backpack, the code you showed is cloning something from a folder and placing it in a folder called Inventory inside the Player instance. If you want to place a tool you should do it in the Backpack, not in a folder inside Player

Tried that, Still the same outcome.

Just place the cloned thing inside Backpack too
itemsfolder[si]:Clone().Parent = player.Backpack

And actually the thing you are saving in the Inventory folder inside Player should not be the tool, should be a StringValue or something similar, so the PlayerRemoving event takes those strings and save them into DataStore

Something like this:

local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage:WaitForChild("ItemsFolder")

game.Players.PlayerAdded:Connect(function(player)
	local succ, invent = pcall(function()
		return inventorydata:GetAsync(player.UserId)
	end) 
		
	local newFolder = Instance.new("Folder")
	newFolder.Name = "Inventory"
	newFolder.Parent = player
	
	if succ then
		if invent then
			warn("player has data")
			for i, si in pairs(invent) do
				if itemsfolder:FindFirstChild(si) then
					
					local stringVal = Instance.new("StringValue")
					stringVal.Name = si
					stringVal.Parent = newFolder
					
					itemsfolder[si]:Clone().Parent = player.Backpack
				end
			end
		else
			warn("player has NO data")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inventory = {}
	for i, si in pairs(player:WaitForChild("Inventory"):GetChildren()) do
		table.insert(inventory, si.Name)
	end
	local success, errormsg = pcall(function()
		inventorydata:SetAsync(player.UserId, inventory)
	end)
	if errormsg then warn(errormsg)
	end
end)
1 Like

this code is working for me now, just tried it out

1 Like

If still not working, add some debug prints, and of course wait for the character to exist:

I tested this one, and works as intended:

local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage:WaitForChild("ItemsFolder")

game.Players.PlayerAdded:Connect(function(player)
	local succ, invent = pcall(function()
		return inventorydata:GetAsync(player.UserId)
	end) 

	local newFolder = Instance.new("Folder")
	newFolder.Name = "Inventory"
	newFolder.Parent = player

	if succ then
		if invent then
			warn("player has data")
			warn(invent)
			for i, si in pairs(invent) do
				if itemsfolder:FindFirstChild(si) then
					local stringVal = Instance.new("StringValue")
					stringVal.Name = si
					stringVal.Parent = newFolder
					
					local theTool = itemsfolder[si]:Clone()
					local CHAR = player.Character or player.CharacterAdded:Wait()
					theTool.Parent = player.Backpack
					warn(theTool, "was added into", player, "backpack")
				end
			end
		else
			warn("player has NO data")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inventory = {}
	for i, si in pairs(player:WaitForChild("Inventory"):GetChildren()) do
		print(si)
		table.insert(inventory, si.Name)
	end
	warn(inventory)
	local success, errormsg = pcall(function()
		return inventorydata:SetAsync(player.UserId, inventory)
	end)
	if errormsg then warn(errormsg)
	end
end)

I tried the script in my game, Didn’t work so i tried using it in a baseplate. Still doesn’t work. Is the ItemsFolder Supposed to carry the items in advance? Or does it fill up after you leave, I can’t tell but either way whenever i leave and rejoin the game prints the table, Its empty and it gives me an errormsg in line 46.

13:41:33.796 {} - Server
13:41:34.504 08DB29CEB8190366.0000000003.08DB29CEEFAAC45E.01

Yes, the ItemsFolder in ServerStorage should contain the Tools.

When player leaves, the script will check the contents of Inventory player’s folder, per thing found in there will use the name to save it into the DataStore.

When player joins, script will search into the DataStore, find the table of strings, then check one by one, using that string to find a Tool in the ItemsFolder which has the same name, then the script will clone that Tool and place it in player’s backpack, and create a stringvalue to place it inside the Inventory player’s folder, in order when player leaves grabs all those strings again and save them into the Datastore

Thats odd i tried the script with and without the tool in the itemsfolder and it still gives the same error, Is it an error on my side?

Start a test and copy whole output and paste it here to check it.
Im gonna record a quick video to show you too.

I’m sorry Start a test? (30Lettersads)