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 8 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 8 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:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")

local inventory = --create table of Inventory items by their unique number/string

game.OnClose = function()
    local playerKey = "user_" .. player.UserId --assuming player is defined
    datastore:SetAsync(playerKey, inventory)
end

and when a player joins...

local dataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")

game:GetService("Players").PlayerAdded:connect(function()
    local playerKey = "user_" .. player.UserId
    inventory = datastore:GetAsync(playerKey)
end)
0
Oh Okay I see it now thenasafarouk 25 — 8y
Ad

Answer this question