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)
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)
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
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)
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
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
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)
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