In this tutorial, I will show you how to save player’s inventory items.
Setup
The way I do it with storing tools, is having a folder in ServerStorage
named Tools
, and all my tools would go there.
A second folder would be a folder containing sub-folders, which would contain each players’ inventory.
Example of hierarchy:
The Inventories
folder would start out empty, but a folder with the name of the player who joins will be created, and the tools they have will be put in there aswell.
Now onto the actual coding!
Loading
Before talking about loading, I will quickly talk about how we will even save in the first place.
We will be saving the name of the tools into a table in this tutorial, but you can always use something else to identify each tool.
Alright back to loading.
When the player joins, just give them their tools:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local player_data = DataStoreService:GetDataStore("player_data")
local tools = ServerStorage.Tools
local inventories = ServerStorage.Inventories
Players.PlayerAdded:Connect(function(client)
local key = "client_" .. client.UserId
local inventory = player_data:GetAsync(key) -- Not worrying about pcalls, do that yourself
local inventory_folder = Instance.new("Folder")
inventory_folder.Name = client.Name
inventory_folder.Parent = inventories
for _, name in ipairs(inventory or { }) do
local tool = tool[name]
tool:Clone().Parent = client.Backpack -- For the player to use
tool:Clone().Parent = inventory_folder -- For saving and loading
end
end)
Not too complicated. We create a new folder of their name. We clone any tools they have into their folder and backpack.
If no data is present, you can either just use an empty table or a starter table of free items.
Saving
When saving, just go through their Inventories
folder. Save all the names of the tools into a table, and save that to the data store. Finally, delete the folder as it is no longer of use.
Players.PlayerRemoving:Connect(function(client)
local key = "client_" .. client.UserId
local tools = { }
local inventory_folder = inventories[client.Name]
for _, item in ipairs(inventory_folder:GetChildren()) do
table.insert(tools, item.Name)
end
player_data:UpdateAsync(key, function(prev)
return tools
end)
inventory_folder:Destroy()
end)
And there we go. Bam. You have a fully functional inventory-saving system. but this tutorial ain’t over yet bro
You might ask how to add items to their inventory in the first place. Generally, games have a shop UI where you can buy items.
So with the power of remotes, we can implement one:
Not the best but just to demonstrate it. When the button is clicked, send a request to the server to buy a tool:
local ui = script.Parent
local frame = ui:WaitForChild("Frame")
local remote = game:GetService("ReplicatedStorage"):WaitForChild("OnRequest")
for _, button in ipairs(frame:GetChildren()) do
button.Activated:Connect(function()
remote:FireServer(button.Name)
end)
end
The way I do it is, I name the buttons the name of the item it corresponds to. So I can just easily run a for loop like that.
The server code simply gives the player the requested item, and adds it to their inventory folder.
local remote = game:GetService("ReplicatedStorage").OnRequest
local ServerStorage = game:GetService("ServerStorage")
local tools = ServerStorage.Tools
local inventories = ServerStorage.Inventories
remote.OnServerEvent:Connect(function(client, request)
local inventory_folder = inventories[client.Name]
local tool = tools[request]
tool:Clone().Parent = client.Backpack
tool:Clone().Parent = inventory_folder
end)
I didn’t code an entire currency system, or add any sanity checks; that is for you to implement.
But there is the tutorial for you. I hope you liked this tutorial, and if you did, please leave a like. It really helps out a lot.
Feedback would be appreciated if I accidentally made a mistake somewhere.