Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
PolyyDev 214 Moderation Voter
7 years ago
Edited 7 years ago

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)
0
local code = {"2004", "6331",} --> to --> local code = {"2004", "6331"} Coasterteam 4 — 7y
0
Oh i'm an idiot, thanks PolyyDev 214 — 7y
0
Actually no, it doesn't work ;-; PolyyDev 214 — 7y
0
Are there any error messages? mustardfoot 90 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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)
0
Works a charm, thanks dude! PolyyDev 214 — 7y
Ad

Answer this question