How to destory a specific amount of tools

SellItem.OnServerEvent:Connect(function(player,Itemname,ItemCount) -- items name, how much items player wants to sell
	local money = player.NonPvPStats.Money
	local Payment = ItemCount * prices[Itemname]
	
	local amount = 0
	for i,tool in pairs(player.Backpack:GetChildren()) do
		if tool:IsA("Tool") and tool.Parent == player.Backpack then
			if tool.Name == Itemname then
				amount += 1
			end
		end
	end
	
	print(amount)
	
	if amount >= ItemCount then
		money.Value += Payment
		for i = 0,amount do
			
		end
		print("Player has enough items ")
	else
		print("Player dosnt have enough items ")
	end
end)

ima go watch anime im kinda stuck

1 Like

Would something like this work for what you need?

SellItem.OnServerEvent:Connect(function(player,Itemname,ItemCount) -- items name, how much items player wants to sell
	local money = player.NonPvPStats.Money
	local Payment = ItemCount * prices[Itemname]
	
	local amount = {}
	for _,tool in pairs(player.Backpack:GetChildren()) do
		if tool:IsA("Tool") and tool.Parent == player.Backpack then
			if tool.Name == Itemname then
				table.insert(amount,tool)
			end
		end
	end
	
	print(#amount)
	
	if #amount >= ItemCount then
		money.Value += Payment
		for i = ItemCount, 1, -1 do
			amount[i]:Destroy()
			amount[i] = nil
		end
		print("Player has enough items ")
	else
		print("Player dosnt have enough items ")
	end
end)

First thing I did was convert amount from a number to a table that contains all the tools of the given itemname. Then if the length of the table is greater or equal to the amount you want to sell, then it’ll do a for loop starting from the amount to sell and going down to 1, where it destroys the tool at that index in the table

2 Likes