Doing a certain task so many times

So I am trying to clear a players inventory slowly (every 0.5s) using an in-game trashcan but I can’t figure out how to go through the number of items in the players backpack. (So that way the status bar and amount of trash go up 1.) Also I can’t figure out how to actually delete a tool. Any help would be appericated and if I need to explain anything in detail lmk!

ModuleScript:

local function TrashItem(Type)
	local TrashValue = TrashcanPath:GetAttribute("TrashValue")
	local Status = TrashcanPath.Hitbox.Capacity.StatusFrame.Status
	if TrashcanPath:GetAttribute("CanBeUsed") == true then
		if Type == "ClearAll" then
			local NumberOfContents = #Backpack:GetChildren()
			TrashcanPath:SetAttribute("TrashValue", TrashValue + 1)
			TweenService:Create(Status, StatusTransitionInfo, {Size = Status.Size + UDim2.fromScale(0.05, 0)}):Play()
			TrashcanPath.Hitbox.Capacity.Capacity.Amount.Message.Text = tostring(TrashcanPath:GetAttribute("TrashValue")).." / 20"
			print("TrashValue: "..TrashcanPath:GetAttribute("TrashValue"))
			if TrashcanPath:GetAttribute("TrashValue") == 20 then
				TrashcanPath:SetAttribute("CanBeUsed", false)
				wait(1)
				TrashcanPath.Hitbox.Capacity.Capacity.Visible = false
				TrashcanPath.Hitbox.Capacity.StatusFrame.Visible = false
				wait(1)
				TrashcanPath.Hitbox.Capacity.Empty.Visible = true
				ReplicatedStorage.GameContent.EmptyTrashPrompt:Clone().Parent = TrashcanPath.Hitbox
			end
            wait(0.5)
		end
	end
end
1 Like

You can use a for i, v in pairs() loop to go through the children of the player’s Backpack. Something like:

for i, item in ipairs(NumberOfContents) do --getting the children of the player’s backpack
-- insert code for increasing the status bar by 1

local haveItem = player.Backpack:FindFirstChild(item.Name) or player.Character:FindFirstChild(item.Name) -- Checking for every item and whether they’re currently equipped or are in the player’s inventory unequipped.

  if haveItem then
   haveItem:Destroy()
  end
end

Got an error on this line.

ReplicatedStorage.Modules.KitchenModules.TrashcanModule:45: invalid argument #1 to 'pairs' (table expected, got number)

Try and replace pairs with ipairs and see if it works.

1 Like

use a for loop and ipairs. the arguments in a standard for loop, typically defined with ‘i’ and ‘v’, stand for index and value. the value is the actual item within the table, and the i is its number (or index) in the table. the table you will provide is the contents of the player’s backpack, which is found with player.Backpack:GetChildren(), as it returns a table with the children as values. to get the maximum number of items however within the backpack, you can use #, which will return the amount of values within a table. an example is local BackpackContents = player.Backpack:GetChildren(); local NumberOfTools = #BackpackContents Remember though, a tool will not be present in the backpack if it is equipped!

for i, v in ipairs(player.Backpack:GetChildren()) do
print(i, v) -- will print the number (index) it is in the backpack, and then the tool (value)
end
2 Likes

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