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

Inventory Save (ERROR? No output messages!)

I have this inventory save script that should work and I think its saving the data but its not loading the data can you tell me what i'm doing wrong?

Here's the script!

local inventoryData = game:GetService("DataStoreService"):GetDataStore("inventoryTest")
local inventoryHolder = script.InventoryHolder

game.Players.PlayerAdded:Connect(function(player)
    local Key = "Player-ID"..player.UserId
    local GetSave = inventoryData:GetAsync(Key)
    
    local DataBaseFolder = Instance.new("Folder", player)
    DataBaseFolder.Name = "Inventory"
    
    for i, inventory in pairs(inventoryHolder:GetChildren()) do
        local newInventory =  Instance.new(inventory.ClassName)
        newInventory.Name = inventory.Name
        newInventory.Value = inventory.Value
        newInventory.Parent = DataBaseFolder
    end
    
    if GetSave then
        for i,v in pairs(GetSave) do
			local testValue = game.ServerStorage.TrailValues:FindFirstChild(v)
			for i = 1, #testValue do 
				if testValue then
					local clone = testValue()
					clone.Parent = DataBaseFolder
					clone.Name = v
				end
			end
		end	
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = "Player-ID"..player.UserId
    local DataBaseFolder = player.Inventory
    local InventoryToSave = {}
    
    for i,v in pairs(DataBaseFolder:GetChildren()) do
        table.insert(DataBaseFolder:GetChildren(), v.Name)
    end
    inventoryData:SetAsync(Key, InventoryToSave)
end)

1 answer

table.insert(DataBaseFolder:GetChildren(), v.Name) is wrong. If you insert your value in DataBaseFolder your table InventoryToSave rest empty! So you save a empty table..

local HttpService = game:GetService('HttpService')

local inventoryData = game:GetService("DataStoreService"):GetDataStore("inventoryTest")
local inventoryHolder = script.InventoryHolder

game.Players.PlayerAdded:Connect(function(player)
	local Key = "Player-ID"..player.UserId
	local GetSave = inventoryData:GetAsync(Key)
	
	local DataBaseFolder = Instance.new("Folder", player)
	DataBaseFolder.Name = "Inventory"
	
	for i, inventory in pairs(inventoryHolder:GetChildren()) do
		local newInventory =  Instance.new(inventory.ClassName)
		newInventory.Name = inventory.Name
		newInventory.Value = inventory.Value
		newInventory.Parent = DataBaseFolder
	end
	
	if GetSave ~= nil and type(GetSave) == 'string' then
		print('JSON detected!')
		pcall(function()
			local decode = HttpService:JSONDecode(GetSave)
			if type(decode) == 'table' then
				print('Decode successfull!')
				for i, v in pairs(decode) do
					local testValue = game.ServerStorage.TrailValues:FindFirstChild(v)
					for i = 1, #testValue do 
						if testValue then
							local clone = testValue:Clone()
							clone.Parent = DataBaseFolder
							clone.Name = v
						end
					end
				end
			end
		end)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Key = "Player-ID"..player.UserId
	local DataBaseFolder = player:WaitForChild('Inventory')
	local InventoryToSave = {}
	
	for i, v in pairs(DataBaseFolder:GetChildren()) do
		table.insert(InventoryToSave, v.Name)
	end

	-- You cannot save string and bool with DataStoreService! Use HttpService:JSONEncode() to save your data
	inventoryData:SetAsync(Key, HttpService:JSONEncode(InventoryToSave))
end)

Theoretically it should work

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