Need some help with a peace of code

I’m using

	for _, i in pairs(SlotsUsed) do
		if SlotsUsed[i] == false then
			HotTemp.Number.Text = i
			SlotsUsed[i] = true 
		end
	end

to find the first item in a table that has a value of false, but it always returns on the textlable when I use

HotTemp.Number.Text = i

Your loop is probably wrong for what you want to do. The loop is set up to go through SlotsUsed which is a table. You discard the key and keep the value. I have to assume that SlotsUsed is indexed by numbers (like 1, 2, 3) and its values are Instances. But if that’s the case why do you look at SlotsUsed[i]?

It looks like you are trying to find the first item in the SlotsUsed table that has a value of false , and then set the Text property of the Number object to the index of that item.

One issue with your current approach is that the loop variable i will always be the value of the item in the table, rather than the index. To fix this, you can use the pairs function to loop through the indices and values of the table, like this:

for i, v in pairs(SlotsUsed) do
    if v == false then
        HotTemp.Number.Text = i
        SlotsUsed[i] = true
        break
    end
end

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