For i,v in pairs only loops only once?

I am making a game like Miners Haven, and I am having quite some trouble making a clear ore system. Basically, I have all of my ores in a certain folder, which I get using folder:GetChildren(), and put that in a for i,v in pairs loop and do v:Destroy() to get rid of them all. However, no matter how many ores there are in the folder, it will only destroy one ore.

I don’t know if I’m missing something or just being stupid, so if you want the code, here it is, but beware, it’s a bit janky:

local deb = 0
script.Parent:WaitForChild("Model"):WaitForChild("Middle"):WaitForChild("ProximityPrompt").Triggered:Connect(function(player)
	local children = script.Parent.Parent.Parent:WaitForChild("MiscInfo"):WaitForChild("OresDropped"):GetChildren()
	if player.Name == script.Parent.Parent.Parent.Owner.Value then
		if deb == 0 then
			deb = 1
			print(#children) -- This returns how many ore is in the folder, I've had values of 5 in the output but yet again it only destroys one of the ore at a time.
			print("Passed Children")
			for i,v in pairs(children) do
				print(i,v)
				v:Destroy()					
				deb = 0
			end
		end	
	end
end)
1 Like

There can be 2 problems:

1: There is only one child in the folder.
2: The script is in that folder and self destruct will happen.

If it is none of these things then you maybe should report an engine bug.

As I said it’s none of those things. You could also know that as I put 4 .parents in and only went down twice. So not the same folder, and I’ve already said there were multiple ores in the folder.

Works fine on my side?!
Does the script work again after the first try?

Yeah I’ve tried it over 10 times with different values and different tables and no use.

Try ipairs instead of pairs. It shouldn’t matter but it’s a shot.

What does this print(#children) show?

EDIT: Actually it might matter, try ipairs

Already tried that, thank you though!

:tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower:

Maybe try another method to get all children :GetDescendants()

Edit: Does it already print for five times?

It shows the number of how many ores there are in the folder, and still only removes one ore.

Nope it prints once.

:tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower:

Maybe there is some sort of yielding.
Try putting task.spawn inside the loop

for i,v in pairs(children) do
	task.spawn(function()
		print(i,v)
		v:Destroy()					
		deb = 0
	end)
end

The [5] is how many ores there are on my base (in the folder right now)
[1 Copper Mine] is the print(i,v)

Edit: It removed one ore but kept the rest

For some reason that worked! Thank you!

Just wait a moment, that was for testing not recommended as a permanent solution…
If the loop is yielding, you’d better checking what is the issue causing it to yield.

Oh wow!!! I removed all variables of debounce and the debounce check and it also worked. Debounce had nothing to do with the loop though? Is that an actual roblox bug lol?

Oh…

:tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower:

So now how do I add debounce without adding it and causing the script to mysteriously break?

Did you try putting the debounce outside the loop? Inside the if statement code block

Yeah I did.

:tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower:

That still removes one only. :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: :tokyo_tower: