How do I save a folder of Tool/item Values in Datastore

I’m trying to make a system where when a player joins the game, they are given an inventory folder, and then as they play the game, the tools that they earn and the amount of tools that they have are saved as a number value in the folder. The initial system works fine, but I can’t seem to find a way to save all the item values in the folder.

image
So here is an example of some items in the player’s inventory, but I don’t know how to save these values and then load them into the folder when the player joins again. I don’t want to individually save values because the game is going to have over 100 items and im not trying to do that.

Any help is appreciated.

2 Likes

i would keep the tools in serverstorage and save the names of the tools the player owns
when the player rejoins you can get the owned tools with the saved names

1 Like

You could serialize the items like this. Take for example you have a sword and you’re trying to save it. Let’s do that while the player is being removed:

game.Players.PlayerRemoving:Connect(function(player)
-- Code
end)

Then, we’d want to make a table to save the data. We’d need to “serialize” the inventory by storing its name and its type by looping (which will be useful later):

game.Players.PlayerRemoving:Connect(function(player)
local Inventory = Player.inventory
local Data = {}
for i, item in pairs(Inventory:GetChildren()) do
if item then
-- If the item exists then we’ll add it to the table
Data[item.Name] = item.Name
end
end
end)

Then we can save it:

game.Players.PlayerRemoving:Connect(function(player)
local Inventory = player.inventory
local Data = {}
local success, errormsg = pcall(function()
-- Check if the code was ran successfully
for i, item in pairs(Inventory:GetChildren()) do
if item then
-- If the item exists then we’ll add it to the table
Data[item.Name] = item.Name
end
end
DataStore:SetAsync(Player.UserId, Data)
end)
if success then
print(“Success!”)
else
error(“There was an error saving “..Player.Name..”’s data!”, 10)
end
end)

Then in the PlayerAdded event we can load the data:

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Inventory = Instance.new(“Folder”)
Inventory.Parent = Player
Inventory.Name = “inventory”
pcall(function() -- So it doesn’t error
DataStore:UpdateAsync(function(oldvalue)
if oldvalue then
-- Add the sword value to the Player’s inventory
for i, item in pairs(oldvalue) do
local Tool = Instance.new(“StringValue”)
Tool.Parent = Player.inventory
Tool.Name = item
end
end
end)
end)
end)

Here’s the full completed script (you can instead modify the code you already have because I doubt you don’t already have a server script):

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Inventory = Instance.new(“Folder”)
Inventory.Parent = Player
Inventory.Name = “inventory”
pcall(function() -- So it doesn’t error
DataStore:UpdateAsync(function(oldvalue)
if oldvalue then
-- Add the sword value to the Player’s inventory
for i, item in pairs(oldvalue) do
local Tool = Instance.new(“StringValue”)
Tool.Parent = Player.inventory
Tool.Name = item
end
end
end)
end)
end)

game.Players.PlayerRemoving:Connect(function(player)
local Inventory = player.inventory
local Data = {}
local success, errormsg = pcall(function()
-- Check if the code was ran successfully
for i, item in pairs(Inventory:GetChildren()) do
if item then
-- If the item exists then we’ll add it to the table
Data[item.Name] = item.Name
end
end
DataStore:SetAsync(Player.UserId, Data)
end)
if success then
print(“Success!”)
else
error(“There was an error saving “..Player.Name..”’s data!”, 10)
end
end)

Hope this helps you out :slight_smile:

Quick note, make sure you enable Roblox API Services in your game so that the DataStore with properly work (Game Settings → Permissions → Enable API Services) :wink:

2 Likes

Hello, I can tell you one way on how to make it save the player tools and load on join.

  1. You would need a folder in ReplicatedStorage/ServerStorage with all the game tools

  2. When the player leaves, you update/create a key on a dataStore with the Player UserId

DataStore:SetAsync(Player.UserId, Tools)

where Tools is a table with all the tools the player has

  1. When the player joins, you can getAsync the DataStore and clone every item on the player inventory folder
1 Like

just realized he was doing values instead of tools

1 Like

Yep. At first I also thought they were tools.

1 Like

Ok don’t use this if u aren’t using tools I haven’t read full

1 Like

Yea, I don’t want to resort to making a folder with all the items in the game because there is going to be a ton of items, and that will take ages. I will try some of you guys’ suggestions, though. Thanks for the support.

2 Likes

Updating the async doesnt work so im gonna use “GetAsync” instead.
image

1 Like

This is what I tried…
image
And this is the error I got…
image

1 Like

Tou have to enable API services in order to make it work

1 Like


I have that on and it still doesnt work.

Wanna give my method a try?

I did try that and not only did I get confused with the script itself but it also didnt work.

Did you get any errors in the output? Also, I updated the full script (cause I realized I made a bunch of errors).

Ok so after doing some more research and trying to find help I stumbled across this video. How to make a Saving Inventory system on ROBLOX! - YouTube

Its a simple explanation and its just what I needed! Here is the script that I ended up doing and it works. Thanks for all the help guys.


Nothing too complicated. Does the task in less than 50 lines of script.

2 Likes

Alright. That’s good, I’m glad that you found an easy solution!

It took me a while to understand the concept of serialization anyways. It’s not the easiest concept to understand at first.

1 Like

Thanks for the help. Maybe someone else with my issue will see this thread and find their solution aswell. :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.