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

I am trying to make a ATM code system using a for i,v loop but only the first code works???

I have written out the code but only the first code in the table works.

local code = {"2004", "6331"} --Only 2004 works as a code
local text = script.Parent.Parent.CodeBar
local val = script.Parent
local plr = script.Parent.Parent.Parent.Parent.Parent

print("ATM Loaded")

script.Parent.MouseButton1Click:connect(function()
	for i,v in pairs(code) do
		if text.Text == v then
			print(plr.Name.." has entered the bank")
			script.Parent.Parent.Parent.PinCode.Visible = false
			script.Parent.Parent.Parent.Page1.Visible = true
		else
			text.Text = "Incorrect Pin!"
			wait(2)
			text.Text = ""			
			
		end
	end 
end)

4 comments

0

local code = {"2004", "6331",} --> to --> local code = {"2004", "6331"}

0

Oh i'm an idiot, thanks

0

Actually no, it doesn't work ;-;

0

Are there any error messages?

1 answer

✓ Accepted answer

I see the error in your script. When you put the code as 6331, it will first check if it says "2004". After seeing it is false, it will set the text to blank two seconds later. After looping, it will then check if the code says "6331", but since you changed the text to become blank, that too will return as false.

Here is a modified version for your script to get it to work.

local code = {"2004", "6331"}
local text = script.Parent.Parent.CodeBar
local val = script.Parent
local plr = script.Parent.Parent.Parent.Parent.Parent

print("ATM Loaded")

script.Parent.MouseButton1Click:connect(function()
    for i,v in pairs(code) do
        if text.Text == v then
            print(plr.Name.." has entered the bank")
            script.Parent.Parent.Parent.PinCode.Visible = false
            script.Parent.Parent.Parent.Page1.Visible = true
			break --Found the code! Breaking the loop.
 		end        
	end
if script.Parent.Parent.Parent.PinCode.Visible == true then --The code was never found in the loop.
text.Text = "Incorrect Pin!"
wait(2)
text.Text = "" 
	end
end)

1 comment

0

Works a charm, thanks dude!

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