How do I save a player's full inventory?

So I know how to save the player’s backpack but I also want to save their equipped tools. The problem is that when the player leaves, the character is destroyed and so is the equipped tool. I tried making the local script below that would update a table in the data saving server.

local player = game.Players.LocalPlayer
local event = game:GetService("ReplicatedStorage").Events.BackPackEvent

player.Backpack.ChildAdded:Connect(function(tool)
	event:FireServer(tool.Name, true)
end)

player.Backpack.ChildRemoved:Connect(function(tool)
	if player.Character:FindFirstChild(tool.Name) then return end
	event:FireServer(tool.Name, false)
end)

player.Character.ChildRemoved:Connect(function(tool)
	event:FireServer(tool.Name, false)
end)

But I think it would be very vulnerable to exploits. Is there a better way of doing this?

You can just listen to these events on the server.

I tried that too but I must’ve been doing it wrong

players.PlayerAdded:Connect(function(player)
	for _, player in pairs(players:GetChildren()) do

		player.Backpack.ChildAdded:Connect(function(tool)
			table.insert(playerInventories, tool)
		end)

		player.Backpack.ChildRemoved:Connect(function(tool)
			if player.Character:FindFirstChild(tool.Name) then return end
			table.remove(playerInventories, tool)
		end)

		player.Character.ChildRemoved:Connect(function(tool)
			if player.Backpack:FindFirstChild(tool.Name) then return end
			table.remove(playerInventories, tool)
		end)
	end
end)

Tried this but it didn’t work, how can I listen to them?

You are re-connecting for every user each time a user joins. Additionally, you are assuming that every user shares the same inventory, which is probably not what you want.

Your script should look a bit like this:

  • create player inventories table
  • when a player joins:
    • add a new table in the player inventories table for this player
    • restore their inventory
    • connect all events - ensure that the child being added is actually a tool!
  • when a player leaves:
    • save their inventory
    • delete their table in the player inventories table

I understand all of that, I just don’t know how to listen for child added event from the server script.

Just do what you were doing before.

Keep in mind:

Selecting Tools from the inventory will equip the Tool, moving it from the Backpack to the player’s character.

Once a character dies, the Backpack is removed and a new one will be created – populating it with the contents of StarterPack and StarterGear.

function connectBackpack(player: Player)
    player.Backpack.ChildAdded:Connect(function(tool)
        -- ...
    end)

    player.Backpack.ChildRemoved:Connect(function(tool)
        -- ...
    end)
end

Players.PlayerAdded:Connect(function(player)
    connectBackpack(player)

    player.ChildAdded:Connect(function(backpack)
        if not backpack:IsA("Backpack") then
            return
        end

        connectBackpack(player)
    end

    -- use a similar approach for the character, as that also gets destroyed and recreated
    -- get the event to connect to: player:GetPropertyChangedSignal("Character")
end)
2 Likes

You are doing it right, but if you are playing it in Roblox Studio itself the script maybe running after you join the server so it does not detect that you are joined. Try it in Roblox Player.

Thank you, this works perfectly!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.