Ive been making an RNG game and it adding the items to the inventory upon rolling is going fine, but I dont know how to save the inventory items. can you help me with this? Thanks!
Hey! I was in the exact same situation.
- My solution was making a table of the items (
invTable
) - on player leave
:JsonEncode(invTable)
and save it. - on player join, load the Json table using
:JsonDecode(loadedJson)
, store it as a variable (loadedJson
) - now you have a table of your items loaded from a datastore called
loadedJson
!
Basically, you store the table as a string (encoding) and transform it to a lua table (decoding) upon loading saved data, since you cant store a whole table.
I cant write a whole script because im on phone, but if you have a question then let me know.
where do i put the invtable and how to save and load and encode?
if you dont know how to save and load data on a datastore then consider watching a tutorial first.
local HttpService = game:GetService("HttpService")
local inventoryTable = {...} -- your table of things in inventory
local encodedTable = HttpService:JsonEncode(inventoryTable)
-- save the encodedTable on a datastore
--- LOADING ---
local loadedTable = datastore:getAsync() -- load the datastore
local loadedInventoryTable = HttpService:JsonDecode(loadedTable)
-- loadedInventoryTable is your previously saved table
Assuming you’ve never done datastores before, and this game is more than just practice, you may want to decide on a datastore module to make things a lot easier to manage.
You can’t go wrong with the ProfileService module.
and where do i put this script?
and is it local?