Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to save inventories of pets?

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.