How do I make this "for i=1,#table do" get the first result that returns "Air"?

So I’ve been working on an inventory system, and There is in total 36 slots. For some reason I cannot understand, This script under, returns the 36th, even though every single slot before that is also “Air”

if AddedToSlot == false then
			local allinv = invslot:GetChildren()
				local findnewslot = false
				print("new slot time")
				for i=1, #allinv do
				if findnewslot == false then
					if allinv[i].Value == "Air" then
							print("Found new slot")
						findnewslot = true
						allinv[i].Value = itemname
						allinv[i].Amount.Value = 1
					end
			end
		   end

I have no idea what makes this return the 36th slot, because every other slot have their value also as “Air”.

1 Like

Try using a for ipairs loop.

if AddedToSlot == false then
	local allinv = invslot:GetChildren()
	local findnewslot = false
	
	for i in ipairs(allinv) do
		if findnewslot == false then
			if allinv[i].Value == "Air" then
				print("Found new slot")
				findnewslot = true
				allinv[i].Value = itemname
				allinv[i].Amount.Value = 1
			end
		end
	end
1 Like

The easiest way to just do the first 35 is to do:

for i = 1, #allinv - 1 do

end

This method will always skip the very last item no matter how big your inventory grows.

If you could share a bit more code I might be able to help understand why you’re getting the current problem, but one thing you can do is add in a break statement inside the for loop once it finds the air value so it stops trying to process.

You could also add a few more print lines, I’d put one on each line, and probably have it output some relevant information to see what’s going on, like the first line of your for loop you can specify to print findnewslot

function inventory.GiveItem(chr,itemname)
	local AddedToSlot = false
	local plr = game.Players:GetPlayerFromCharacter(chr)
	if plr ~= nil then
	print(plr.Name.." will recieve the item: "..itemname.."")
	local invslot=plr:FindFirstChild("InventorySlots")
		if invslot ~= nil then
			print("invslots found")
		local allinv = invslot:GetChildren()
			for i=1, #allinv do
				if AddedToSlot == false then
			if allinv[i].Value == itemname then
				if allinv[i].Amount.Value <= 64 then
						allinv[i].Amount.Value = allinv[i].Amount.Value + 1
						print("added to a different slot")
						AddedToSlot = true
					else
						end
					end
			end
			end
		end
		if AddedToSlot == false then
			local allinv = invslot:GetChildren()
				local findnewslot = false
				print("new slot time")
				for i=1, #allinv do
				if findnewslot == false then
					if allinv[i].Value == "Air" then
							print("Found new slot")
						findnewslot = true
						allinv[i].Value = itemname
						allinv[i].Amount.Value = 1
					end
			end
		   end
		end
	end
end

The full section.

What is going wrong, is that it chooses the last answer, I don’t want it to SKIP the 36th, I want it to get the FIRST one that returns “Air”

Still returns 36. (I have to type something extra here, so it actually lets me reply with this)

add a break after your addedToSlot line your line:

AddedToSlot = true
break

Reference:

Still returns 36. (I have to add more text again aa)

Will, you consider converting some of your code to functions to make it easier to read and diagnose?

It either checks for if there’s a stack to join or to start a completely new stack, thats literally it, the stack part of it works fine, but starting a new stack always starts it at 36.

I wonder if this is because GetChildren isn’t guaranteed to get all children in order. If all the children of invslot can be renamed 1, 2, 3, etc, you can do this.

local inv1 = invslot["1"]

for i = 1, 36 do
    ...
    if invslot[tostring(I)].Value == "Air" then
1 Like

you are letting the loop continue after it found it so do this.

					if allinv[i].Value == "Air" then
						print("Found new slot")
						findnewslot = true
						allinv[i].Value = itemname
						allinv[i].Amount.Value = 1
						break
					end

Before I try to use your method, I tried to print debug, and it does print every single number from 1 to 36.

Still returns 36. (AaAaAaAaAa more letters so I can respond)

Are you sure about that?
This is supposed to end it, try printing the value and the index.

And try using this, it’s better in my opinion.

for _, v in pairs do

end

Pairs is good but I’m more used to for i=1, #TABLE do
Back to the point. It still returns 36.

1 Like

Have you tried to printing allinv[i].Value before checking if it is equal to Air?

Yeah, and I’ve only now understood that it begins with 36. I need to redo the order now