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: :slight_smile:](https://doy2mn9upadnk.cloudfront.net/images/emoji/twitter/slight_smile.png?v=12)
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: :wink:](//devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/3X/8/e/8ec9317d6d952b271c47f40b20836ea392bef673.png?v=12)