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

How to save inventories of pets?

Asked by
edwisc 1
2 years ago

Hi, I'm struggling to save inventories of pets that a player can hatch. I've tried multiple tutorials and I can't seem to get anything to work/

local dataStoreService = game:service'DataStoreService'
local dataStore = dataStoreService:GetDataStore'HatcherGame_2'
local data = {}
local number = 0

local function saveData(pet)
    table.insert(data,pet)
    dataStore:SetAsync('Petting_Key_'..number,data[number])
end

local function loadData()
    local load_data = dataStore:GetAsync('Petting_Key_'..number)
    for i,v in pairs(load_data)do
        print(i,v)
    end
end

game.Players.PlayerAdded:Connect(function(player)
    if dataStore:GetAsync('Petting_Key_'..number) == nil then
        print("NO DATA")
    else
        loadData()
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    for i,v in pairs(plr.Inventory:GetChildren()) do
        print(i)
        print(v)
        number = number + 1
        saveData(v)
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago

If you're trying to save a table or array etc. I found using JSONEncode (to encode the table into a string to be saved) and JSONDecode (to decode the saved string for use) which are part of HTTPSService makes the process easy!

Also look at "BindToClose" so the data of the last player to leave will be saved properly too :)

Ad

Answer this question