Is there a way to make an exact inventory save?

this is my script and whenever i put the tools in my backpack and rejoin, the tools will pop back up in my starterpack(toolbar)

local itemsFolder = game:GetService('ServerStorage'):WaitForChild('Items')
local players = game:GetService('Players')
local dataStoreService = game:GetService('DataStoreService')
local mainStore = dataStoreService:GetDataStore('Main')

local function playerAdded(plr)
	repeat
		task.wait()
	until plr.Character
	task.wait(0.1)

	local success, inventory = pcall(function()
		return mainStore:GetAsync(plr.UserId)
	end)

	if success and inventory then
		for _, name in pairs(inventory) do
			local item = itemsFolder:FindFirstChild(name)
			if item then
				local clone = item:Clone()
				clone.Parent = plr.Backpack
			end
		end
	end
end

local function playerLeft(plr)
	local backpack = plr.Backpack
	local char = plr.Character
	local itemsTab = {}

	if char then
		for _, object in pairs(char:GetChildren()) do
			if object:IsA('Tool') then
				table.insert(itemsTab, object.Name)
			end
		end
	end

	for _, object in pairs(backpack:GetChildren()) do
		if object:IsA('Tool') then
			table.insert(itemsTab, object.Name)
		end
	end

	local success, err = pcall(function()
		return mainStore:SetAsync(plr.UserId, itemsTab)
	end)

	if not success then
		warn("Error saving data for player " .. plr.UserId .. ": " .. err)
	end
end

players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerLeft)
1 Like

I just checked your script, and it works for me (i added tool in itemsfolder to clone it, and another one added in-game from server to backpack to save it and get in the next game). The result was normal, I got tool in my backpack. I tried to add another tool and it works. Don’t you have by any chance another scripts in your game or something?

1 Like

try putting the tools in ur backpack and rejoin

I’m not sure what you want, you mean you want it to appear outside of the hotbar right? I don’t think that’s possible because roblox’s built in tool system will automatically put the tool in the hotbar no matter what because it will go from 1-10 in order. I don’t really know how to explain it but I think you know what I mean.

I don’t understand why it’s an issue that it doesn’t go in the backpack, but then you need to make a custom inventory system like other games then rather than using roblox’s built in one.

i have my own custom inventory system and im trying to work on an exact inventory save system because whenever the player’s inventory is full and properly set (the weapons are in the toolbar) and when they leave and rejoin the inventory is all mixed again

Try and save the position of the tools in the inventory system then, you can probably just use a value to identify it like numbers for example. Then use a for loop with the saved-data.

1 Like