local ServerStorage = game:GetService("ServerStorage")
local InventoryTemplate = ServerStorage:WaitForChild("InventoryTemplate")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local function SetupInventory(player)
local Inventory = player:WaitForChild("Inventory")
local PlayerGui = player:WaitForChild("PlayerGui")
local MainGui = PlayerGui:WaitForChild("MainGui2")
local InventoryGui = MainGui:WaitForChild("InventoryGui")
for i, item in pairs(Inventory:GetChildren()) do
local itemGui = InventoryGui.Templates.Item:Clone()
itemGui.Name = item.Name
itemGui.ItemName.Text = item.Name
itemGui.ItemQuantity.Text = item.Value
if item.Value > 0 then
itemGui.Visible = true
itemGui.Parent = InventoryGui.ItemList
else
itemGui.Visible = false
itemGui.Parent = InventoryGui.ItemList
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local GameData = InventoryTemplate:Clone()
GameData.Name = "Inventory"
GameData.Parent = player
SetupInventory(player)
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-GameData")
end)
if success then
GameData = data
else
print("There was an error getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-GameData",player.Inventory)
end)
if success then
print("Data Saved")
else
print("Error saving data")
warn(errormessage)
end
end)
You could have a folder of every single tool in the game inside of ReplicatedStorage or something. Then, just save the names of all of the tools in their backpack. When they rejoin, just clone the tools by searching through the folder.
If you have the folder already, no need to make another. You could do some code like this for when they are leaving:
local savedItems = {}
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
table.insert(savedItems,v.Name)
end
end
myDataStore:SetAsync(player.UserId.."-GameData",savedItems)
Then for the code when they are rejoining later could be something like:
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-GameData")
end)
local AllItemsInTheGame = game.ReplicatedStorage.Items:GetChildren()
if success then
if data then
for i,v in pairs(data) do
if table.find(AllItemsInTheGame,v) then
local tool = AllItemsInTheGame[v]:Clone()
tool.Parent = player.Backpack
end
end
end
else
print("There was an error getting your data")
warn(errormessage)
end
Just make sure there is a folder in ReplicatedStorage named Items, and it has all the tools. This is one way I would approach it, although I am not in studio right now so I have no clue if any of this code works. I think it should, but I am not that good with tables.
local savedItems = {}
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
table.insert(savedItems,v.Name)
end
end
myDataStore:SetAsync(player.UserId.."-GameData",savedItems)
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-GameData")
end)
local AllItemsInTheGame = game.ReplicatedStorage.Items:GetChildren()
if success then
if data then
for i,v in pairs(data) do
if table.find(AllItemsInTheGame,v) then
local tool = AllItemsInTheGame[v]:Clone()
tool.Parent = player.Backpack
end
end
end
else
print("There was an error getting your data")
warn(errormessage)
end
THIS IS FOR LEAVING THE GAME:
local savedItems = {}
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
table.insert(savedItems,v.Name)
end
end
myDataStore:SetAsync(player.UserId.."-GameData",savedItems)
The “Tools” are resources in the game like wood that you can collect in game, so i don’t think it should be added to the players backpack? Or is that needed?
Im not really sure how you made your inventory. Instead of looping through their backpack, search through whatever is in their custom inventory. Ive never made a custom inventory before, so I cannot really help you that much
About the part where you asked if you need to add it into your backpack, just add it back into the custom inventory. I assume it is a table.
Alright then. Just have a folder with all the image labels and stuff in replicated storage. Use my code to save the names of all the image labels when they leave. Then when they rejoin, clone all the image labels by searching for them in the folder. Parent them to the player’s inventory gui or whatever and boom, its done!