Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

For loop goes through only 1 time instead of 20?

local slotBase = 20

for i = 1,slotBase,1 do
	if #items > 0 then
		for index,value in pairs(items) do
			if value.Slot == "Slot" .. i then
			end
			if index == #items and value.Slot ~= "Slot" .. i then
				local place = #items + 1
				table.insert(items, place, Slot)
				items[place].Slot = "Slot" .. i
				save()
			end
		end
	else
		local place = #items + 1
		table.insert(items, place, Slot)
		items[place].Slot = "Slot" .. i
		save()
	end
end

I set up a print, and it said the loop only went through one time. It is supposed to go through 20 times. Help to fix this problem is highly appreciated, thanks!

2 comments

1

TheAlphaStigma's answer is correct. The only reason it printed once is because you are waiting for the INDEX to hit the NUMBER OF ITEMS IN THE TABLE, which is most likely 20. If it only hits 20 once, it'll only execute the action once. Read his post again.

0

@Shawnyg Good on ya, lad! :D

1 answer

Your problem may be that line 8, in your code, is set up to check if the value "index" is equal to the number amount in the table "items," and not the items themselves.

To fix this, I believe you should (maybe?) change #items on line 8 to the current iteration, 'i', if that's what you're intending. (I'm not sure how you're using the code, so I'm going off an assumption.)

Hope this helped you in any way? ':D

8 comments

0

It most clearly says #items

0

Oops. lol

0

Edited & fixed that. :P

0

Also #items is a table (not shown)

0

Thanks for trying though

0

I'm aware it's a table: that's why I pointed out '#items,' b/c it's returned the number of items in the table. lol

0

Which I am also aware of and that is how my script works..

0

Then why'd you say #items was a table when I already knew that..? Also, I didn't say that the 'items' table needed changing/ to be removed, I pointed out the problem w/ line 8.

This discussion is preserved as it was. New answers, comments, and votes are closed.