Checking for missing items in player's inventory

Currently trying to check if an item from a player was deleted somehow and if so then remove it from the player’s gui, as a test im running it in a loop every 1 second but it would be much preferable if it was triggered only when an item from the player’s inventory was deleted

invEvent = game.ReplicatedStorage.InventoryEvent
plr = game:GetService('Players').LocalPlayer
inv = plr:FindFirstChild('Inventory')
parent = script.Parent

while task.wait(1) do

	for i,child in pairs(inv:GetChildren()) do
		for i,item in pairs(parent:GetChildren()) do

			if child.Name ~= item.Name and item:IsA('TextButton') and item.Name ~= 'Sample' then
			--	print(item.Name.." not found in "..plr.Name..' Inventory')
				--item:Destroy()
				print(child)
				
			end
		end

	end

end

You could detect if a child is added or removed from the player’s backpack with an event.
Then if its removed from the backpack, check to make sure its not put under the character (meaning you are holding it)
If thats not the case, the item is removed.

2 Likes

you can use a childremoved event to detect with an object is removed, and you can use a childadded event to check when an object is added

inv.ChildAdded:Connect(function(Object)
print(Object)
end)
inv.ChildRemoved:Connect(function(Object)
warn(Object)
end)
1 Like

ah nice, ill try that and see if i get a result

1 Like

You can fire a check function any time you remove, add or make changes to your inventory.

If you’re trying to replicate it from server to client, use a RemoteEvent

1 Like

script seems to work fine without using remote even since once its gone from the server its also gone from the client inventory, not the other way around of course, but i don’t think that will ever happen… so…

thank you I got the code working !

invEvent = game.ReplicatedStorage.InventoryEvent
plr = game:GetService('Players').LocalPlayer
inv = plr:FindFirstChild('Inventory')
parent = script.Parent

inv.ChildRemoved:Connect(function(child)
	print(child.Name)
	for i,item in pairs(parent:GetChildren()) do
		if child.Name == item.Name and item:IsA('TextButton') and item.Name ~= 'Sample' then
			item:Destroy()
		end
	end

end)
1 Like