Datasore purchase history, Help with storing tables in datastore

  1. What do you want to achieve? I want to make a script that detects puchases made in-game and store them in a datastore under the userid of the player who made the purchase.

  2. What is the issue? i do not know how to store tables?

  3. What solutions have you tried so far? i tried finding similar topics, but i can’t find any or i didn’t understand any…

I want this script to also detect the time the purchase was made + the assetid.

Hi there, be sure to share any code you’ve tried or any resources you’ve looked at when posting :slight_smile:
Regardless:

Tables can be saved to datastores the same way that strings, numbers and booleans can, so long as they only store strings, numbers and booleans.

Let’s go through what you want to store first

-- here's a table for all your table-ing needs
local PurchaseHistory = {

}
-- this is our first table, but we want to store tables within tables here
-- so, let's say we purchased something cool, and we want to keep data on that purchase
-- we would insert that data as a table into "PurchaseHistory" like so

PurchaseHistory["SomethingCool"] = { -- Given the name "SomethingCool" so it's easy to find
    ["TimeOfPurchase"] = CurrentTime -- I think you can use os.time() to get a timestamp.
    ["AssetId"] = AssetId -- speaks for itself
}

--You could also index these by their AssetId, and have the value as just the timestamp.

-- ALTERNATIVELY, if you're not feeling like fancy names you can do this:
table.insert(PurchaseHistory,{ -- No name, just inserting the table    
    ["TimeOfPurchase"] = CurrentTime
    ["AssetId"] = AssetId
})
-- If you only plan to iterate through, this is fine.
-- However, when attempting to find specific tables, I prefer to index them by name

Finally, you would need to insert the PurchaseHistory table into your datastore table so you can store other things too.

As for loading and saving data, there’s two lovely little functions you can get from GlobalDataStores which you should read into and become familiar with!

GetAsync (for loading data)

SetAsync (for saving data)

Hopefully you find this helpful :slight_smile:

1 Like

omg you’re so nice, thank you so much! :heart: :sob:

1 Like

https://developer.roblox.com/en-us/api-reference/callback/MarketplaceService/ProcessReceipt
I’d recommend checking out the example script provided here.