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

Inventory Datastore not saving?

I'm making a custom inventory but it won't save! It says that it's saving but it's not. Here is the script.

local ds = game:GetService("DataStoreService"):GetDataStore("Inventory")
game.Players.PlayerAdded:Connect(function(p)
	wait()
	local inv = Instance.new("Folder",p)
	inv.Name = "Inventory"
	local success,fail = pcall(function()
		local items = ds:GetAsync(p.UserId)
		if items then
			for i,v in pairs(items) do
				if v then
					if game.ReplicatedStorage.Items:FindFirstChild(v) then
						game.ReplicatedStorage.Items[v.Name]:Clone().Parent = inv
					end
				end
			end
		end
	end)
	if success then
		print("Got items!")
	else
		print("Didn't get items, possibly no items!")
	end
end)
game.Players.PlayerRemoving:Connect(function(p)
	local success,fail = pcall(function()
		local itemstosave = {}
		for i,v in pairs(p.Inventory:GetChildren()) do
			if v then
				table.insert(itemstosave,v.Name)
			end
		end
		ds:SetAsync(p.UserId,itemstosave)
	end)
	if success then
		print("Saved!")
	else
		print("Not Saved :(")
	end
end)

1 answer

not 100% sure this will work but it's worth a try.

local ds = game:GetService("DataStoreService"):GetDataStore("Inventory")
game.Players.PlayerAdded:Connect(function(p)
    wait()
    local inv = Instance.new("Folder",p)
    inv.Name = "Inventory"
    local success,fail = pcall(function()
        local items = ds:GetAsync( tostring(p.UserId))
        if items then
            for i,v in pairs(items) do
                if v then
                    if game.ReplicatedStorage.Items:FindFirstChild(v) then
                        game.ReplicatedStorage.Items[v]:Clone().Parent = inv --i think it errors here because your saving strings and your trying to get the name of the string
                    end
                end
            end
        end
    end)
    if success then
        print("Got items!")
    else
        print("Didn't get items, possibly no items!")
    end
end)
game.Players.PlayerRemoving:Connect(function(p)
    local success,fail = pcall(function()
        local itemstosave = {}
        for i,v in pairs(p.Inventory:GetChildren()) do
            if v then
                table.insert(itemstosave,v.Name)
            end
        end
        ds:SetAsync( tostring(p.UserId) ,itemstosave)
    end)
    if success then
        print("Saved!")
    else
        print("Not Saved :(")
    end
end)

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