Problem Stopping For Loop

I’m not really good at scripting and horrible with tables, and I need help stopping a loop. Basically what is happening is that when a textbutton is clicked, it searches through a table in a module script and sees if what’s typed in a textbox is the same as an answer for one of the codes. However, when this is done, it does not stop the loop when something correct is typed in, making everything become empty again.

Picture of the modulescript:

Picture of the textbutton code:

Any help?

Actually, the code looks fine, it should work.
Maybe you’re writing the code incorrectly?
What does the output say?

And what structure is there? Like a photo of the instances.

And you should paste the code into a code block.

I noticed you use else a lot in a few of your posts now. Not that it is bad, but it is not always necessary and can lead to problems if used when it is not needed.

Here you have two spots where it is not needed at all if things were worded a bit differently, and it may be leading to the error you are encountering.

If this was setup to break the for loop when it hit a correct word, that may change everything.
Hard to say from a few pictures of a few code chunks.. Use the <> option and post the script.

What is the point of the else statement?
Just do an early return and don’t do such a war crime to humanity anymore.
Cache instances and use the workspace global
Dont use pairs/ipairs and use generic iteration instead

Can you please send code next time?
I’m not in the mood to waste my time retyping it.

Just capture value.

local Result:string?
for i,v in codes do
if textb.Text==v.answer then Result=codes.result break end
end

print(Result)

Check if it’s a truthy value, like if Result then
After all, since Luau is a dynamic language, don’t let it go to waste.

don’t use pairs, for one. what you have should should work though.
suggested improvement:

-- Please don't do this.
if game.Workspace.Soundeffects.Enter.IsPlaying == true then else
    game.Workspace.Soundeffects.Enter:Play()
end
-- Step 1: use workspace, not game.Workspace
if workspace.Soundeffects.Enter.IsPlaying == true then else
    workspace.Soundeffects.Enter:Play()
end
-- Step 2: remove the unneeded == true
if workspace.Soundeffects.Enter.IsPlaying then else
    workspace.Soundeffects.Enter:Play()
end
-- Step 3: use NOT, not ELSE.
if not workspace.Soundeffects.Enter.IsPlaying then
    workspace.Soundeffects.Enter:Play()
end
-- Step 4 (optional): use a variable to make things prettier
local enterSound = workspace.Soundeffects.Enter
if not enterSound.isPlaying then
    enterSound:Play()
end

Your else statement is being ran for every code that doesn’t match, which is going to cause a knock-on effect of every other code being invalid, too, unless you happen to be very lucky and hit the first code in the list.

You should move any “finalizer” code like you have there to outside of the loop. Everything else is more or less fine. This is a basic linear search algorithm you’re doing here.

Step through your code and consider what happens to textb when it fails to match. You have textb.Text = "", so now the loop is going to go to the next element, and it’s going to test testb.Text == code.answer, but we know that textb.Text was just set to "", so we are going to repeat this cycle.

yeah it’s been a while since i’ve made a post sorry for not making it into a code block I forgot

i’ve gotten other complaints about this in other comments too lol

thanks for your advice, it’ll probably hurt you hearing this but I’ve been doing it my way literally the entire time I’ve ever made a game, now I’ll make sure to do it your way more efficient way!

yeah I just made a variable be equal to true once a code was found, and then put the “invalid” section outside of the for loop for it to work like you said. thank you!

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