Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you store inventory in the cloud?

Asked by 9 years ago

I want to store purchased inventory somewhere in the cloud (DataStore.) How do I do so?

1 answer

Log in to vote
2
Answered by 9 years ago

You can't store Instances in DataStore (unlike with Data Persistence).

This is how I would do it, as long as each item of the same type had the same statistics. For example, all Iron Swords do the same amount of damage and have the same reload time. If this is the case, use the below method. If not, it is a lot more complex.

Give each individual item a number, or give each individual item a special string name, and save it in DataStore. For example:

1local dataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")
2 
3local inventory = --create table of Inventory items by their unique number/string
4 
5game.OnClose = function()
6    local playerKey = "user_" .. player.UserId --assuming player is defined
7    datastore:SetAsync(playerKey, inventory)
8end

and when a player joins...

1local dataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")
2 
3game:GetService("Players").PlayerAdded:connect(function()
4    local playerKey = "user_" .. player.UserId
5    inventory = datastore:GetAsync(playerKey)
6end)
0
Oh Okay I see it now thenasafarouk 25 — 9y
Ad

Answer this question