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

How can I use different values from a table each time I click on a brick?

I need help with a script I'm making, everytime I click on a brick I'd like it to go through the table at the top 1 by 1. So first click on the brick it would print 1, second click it would print 2.. ect. Any help will be appreciated!

This just prints every time I click, I need it to wait for another click and not print the code before it, I just want the one table value that its currently on

Table = {1,2,3,4,5}

function Click ()

	for i = 1,1 do
	
		print (i)

 	for i = 2,2 do
	
		print (i)
	
	for i = 3,3 do
	
		print (i)

 	for i = 4,5 do

		print (i)
 	
	for i = 5,5 do

end
end
end
end
end

1 answer

✓ Accepted answer

Your current code

Every time you run the click function it will run all of your loops as they are nested. Please also tab your code as it helps keep track of where the 'end' is e.g.:-

Table = {1,2,3,4,5}

function Click()

    for i = 1,1 do -- 1st loop
        print (i)

	    for i = 2,2 do -- nested loop
	        print (i)
	
		    for i = 3,3 do
		        print (i)
		
			    for i = 4,5 do
			        print (i)
			
				    for i = 5,5 do
				
					end
				end
			end
		end
	end
end

Click()

An example

You can use a counter to store the index e.g.:-

local table = {1,2,3,4,5,6,7,8,9}
local i = 1 -- tables start from index 1 in Lua

function click()
	i = i + 1 -- increment our counter
	
	if table[i] ~= nil then -- check that the index exists in the table e.g. is not nil
		print('current value', i)
	else
		print('i is outside the range of the table')
	end
end

This is an example only, hope this helps.

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