So Today I wanted to make a system that saves your Inventory when you leave the game, I tried some stuff and it didn’t work I looked up almost all forums on this and found nothing, I read the wiki and other. The System should save your In-Game Inventory like your Crates to open them later. I hope there is anyway to do this and I hope It’s not complicated.
What kind of system do you use for your inventory? Does it have physical objects?
If you use ValueBases
, you can loop through them and save their Name
and Value
to a table. Then when the player leaves, save the table to their data store. When they join back, load their data and scan the table and apply the crates accordingly.
I used none becuse no system worked I tried using an Instance but It gave me an error that It should be a number.
Oh, you can’t save Instances or certain userdatas to DataStores. Your crate system should have functions that create ValueBases
under a folder in the Player
Instance or something similar. And you should have another function that reads the Values in that folder and proceed to generate the crates to the player’s inventory.
Example
local function create_crate(name: string, rarity: string, player: Player)
-- create the crate
end
local function read_value_data(player: Player)
-- function that reads the ValueBases for the player
local folder = player:FindFirstChild("Crates")
if folder then
for _, object in pairs(folder:GetChildren()) do
-- loops through the folder to find the values
if object:IsA("ValueBase") then
create_crate(object.Name, object.Value, player)
-- call the function to create the crate
end
end
end
end
Ok I will see if this works and btw the system is for everything not just crates
uve literally just got to save the names of the items in a table to the data store and then when you want to for example when the player joins usually you’ll go to their data in the data store and then loop through the table and clone everything from rep storage into their inventory (or from where ever you store the inventory items)
It saves nothing in the folder
What system did you use to save it?
P.S. Data Stores don’t work correctly in studio
But It will save stuff to like things you built and thats the stuff that doesn’t exist then
you dont save instances to the datastores you save strings or strings in tables to the data store if you want to save stuff from a folder you will convert all the data in the folder to string format and save that
instances being
- folders
- welds
- parts
ect
the script you send me. It’s going to save other stuff too btw
The script I sent was for an example of how your system is suppose to be structured. For it to save, you need to use DataStoreService
and save the values not the objects.
Example
local dataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
local function save_player_data(player: Player)
-- function to save the player's data
local folder = player:FindFirstChild("Crates")
if folder then
local data = {}
for _, value in pairs(folder:GetChildren()) do
-- loop through the objects and save their **info**
data[value.Name] = value.Value
end
local saved, output = pcall(function()
DataStore:SetAsync("key-"..player.UserId, data) -- save their data
end)
if saved then
-- if it saved
else
-- if it failed
end
end
end
It’s not going to save a value It’s going to save a part
For it to save a part, you need to serialize it. And condense things like Position
and CFrame
to arrays. It’s gonna take a bunch of functions and tables, so I can only show you a part of it.
Example
-- just part of serialization
local function serialize(object: Instance)
local properties = { "Name" , "Position" , "CFrame" , "Parent" , "BrickColor" , "Size" , "Material" }
-- table that holds the properties to the be serialized
local function object_has_property(prop: string) : boolean
return pcall(function()
return object[prop]
end)
end
-- ...
end
I serialize objects by storing a dictionary with the part’s class that holds an array with the properties to save. From there, I loop through it and call functions to change CFrames and Vector3s to arrays that can be loaded later. The functions are also recursive and work independently depending on the situation. A large object can cause the script to yield.
but what If It’s a model? do I have to do something else?
You do the same process except scanning through it’s children and calling the function on them
Consider InstanceStore
Ok thanks this should work and It did thanks
Is there any tutorial how to do this because I have no idea I read the post and tried many ways