Table value not changing when it should

I want to cycle through a table and find the next value after finding a value that is true, it does exactly that, however the “next value” i was talking about doesnt change and I dont know why? I printed it and it doesnt seem to change. I have not tried any solutions since I don’t really know what to do.

    local module = {}

function module.CreateGUI(Text, cd)

	local tables = {
		true,
		false,
		false,
		false,
		false,
		false,
	}

	for i,v in pairs(tables) do
		if tables[i + 1] == false then

			print(i + 1, tables[i + 1])
			tables[i + 1] = true
			-- take note that the script runs through this scope so it should change
			break
		end
	end
end

return module

because, you print the value before you change it

3 Likes

Okay ive already did the opposite it was just a old script mb, anyways it still doesnt change

you are breaking out of the for loop on the first iteration

it should set the tables[2] to true and then break

that is… well exactly what i did, could you elaborate if im misunderstanding

okay you are telling me that functionality is intended. my bad.

What happens if you try to print out tables[i+1]?

okay so now its sending true, but its stuck on the 2nd value for some reason

what does that mean “stuck”, it’s functioning exactly as intended

if you run the code above the result tables should look like this

local tables = {
    true,
    true,
    false,
    false,
    etc..
}

So i printed it out on both sides of tables[i + 1] = true, and the 2nd value of the table only changes in the scope of the for loop and not on the actual module

Hence it caused a endless loop and broke it, not sure how to fix

you are trying to say in this scope of the CreateGUI function

image

Okay what i am saying is: When i change the value in the scope of the for loop, it does not change the value on the table which is outside of the for loop

Did you for some reason call the function many many times ?

Nope i only called it once and thats it

if i tkae that out the for loop wont function though

Well I don’t know what to tell you, did you try printing the table outside the for loop and it didn’t change?

Well, whatever i did basically proved it so yes.

Please post your updated code, I have a feeling you are running different code than you showed in the op


So i only changed the for loop part

Where are you printing tables outside of the for loop?

You are telling me it’s not changing the table but you don’t know that because you are not printing the table, or you didn’t include it in the screenshot.

the first print will print “false” the second print will print “true”

You can even print tables inside the for loop and you will see it look like

tables = {
   true,
   true,
   false,
   false,
}