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

How do I stop this for loop from firing my function multiple times?

Asked by 2 years ago

Hello, me again.

For the past few weeks, maybe even months, I've been working on a keycard door . I was trying all sorts of different stuff to make it work, but today I managed to get a major breakthrough. My code finally worked. It correctly detected a IntValue inside all of my 6 keycards and then acted on what Number the value was. However, In order to search through the entire backpack, I used a for loop . This now makes the function fire 6 times due to me having 6 keycards, and the door opens multiple times. This of course is not what I want. I want it to only fire once if the Localplayer is the possession of a valid keycard , and if not, then it will stay closed.

well enough talking and more scripting. Ive added alot of notes to my script to make the understandment a bit more easier. The following script is the part of my script that is causing the trouble. Here:

--------------------------------- The first Keycard Reader ------------------------
script.Parent.Parent.KeycardReader1.ProximityPrompt.Triggered:connect(function(tool)
    ------------------------ checking for the value in the keycards --
    local check = tool.Backpack:GetChildren()
    for i = 1, #check do -- begin of loop
        local Card = check[i] 
        local Value = Card.Keycard.Value -- Crucial Value defining the clearance
        if Value == 5 or Value == 4 or Value == 3 then -- correct clearances 
            bool = false
            print("Valid")
            script.Parent.AccessGranted:play()
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright green")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright green")
            openDoor()
            wait(1)
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright red")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright red")
        elseif Value == 2 or Value == 1 then -- false clearances
            bool2 = false
            print ("Invalid")
            script.Parent.AccessDenied:play()
            wait(1)
        end
    end-- end of loop (loops 6 times due to 6 keycards)
end)

The reason I just do not simply put the for loop out of the function is because then the Locals of "Card" and "Value" will be unknown due it being a property of the for loop. My question being: How do i stop this for loop?

just for good measures, here is the entire script.

------------------ Tweenstuff, ignore this -----------
local TweenService = game:GetService("TweenService")                            

local door = script.Parent

local tweenInfo = TweenInfo.new(
    2,
    Enum.EasingStyle.Quart,
    Enum.EasingDirection.InOut,
    0,
    false,
    0
)

local OpenGoal = {
    CFrame = door.CFrame * CFrame.new(0, 0, door.Size.Z)
}
local ClosedGoal = {
    CFrame = door.CFrame
}

local TweenOpen = TweenService:Create(door, tweenInfo, OpenGoal)
local TweenClosed = TweenService:Create(door, tweenInfo, ClosedGoal)

local bool = true
local bool2 = true
------------------------------------------------------------------------------------------------------
local clearance = { -- here i tried to define the correct and incorrect clearances which didnt use much. maybe you can do something with this 
    ["1"] = false,
    ["2"] = false,
    ["3"] = false,
    ["4"] = true, 
    ["5"] = true, 
    ["6"] = true,

}

------ Opening of the door ------- (no problems here)--------
function openDoor()
    script.Parent.Parent.KeycardReader1.ProximityPrompt.Enabled = false
    script.Parent.Parent.KeycardReader2.ProximityPrompt.Enabled = false
    script.Parent.DoorOpen:play()
    TweenOpen:Play()
    wait(3)
    TweenClosed:Play()
    script.Parent.DoorClose:Play()

    script.Parent.Parent.KeycardReader1.ProximityPrompt.Enabled = true
    script.Parent.Parent.KeycardReader2.ProximityPrompt.Enabled = true
end
----------------------------------------------------------------------------------
--------------------------------- The first Keycard Reader ------------------------
script.Parent.Parent.KeycardReader1.ProximityPrompt.Triggered:connect(function(tool)
    ------------------------ checking for the value in the keycards --
    local check = tool.Backpack:GetChildren()
    for i = 1, #check do -- begin of loop
        local Card = check[i] 
        local Value = Card.Keycard.Value -- Crucial Value defining the clearance
        if Value == 5 or Value == 4 or Value == 3 then -- correct clearances 
            bool = false
            print("Valid")
            script.Parent.AccessGranted:play()
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright green")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright green")
            openDoor()
            wait(1)
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright red")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright red")
        elseif Value == 2 or Value == 1 then -- false clearances
            bool2 = false
            print ("Invalid")
            script.Parent.AccessDenied:play()
            wait(1)
        end
    end-- end of loop (loops 6 times due to 6 keycards)
end)

--------------------------------- The second Keycard Reader (You can ignore this its the same thing) ------------------------
script.Parent.Parent.KeycardReader2.ProximityPrompt.Triggered:connect(function(tool)
    ------------------------ checking for the value in the keycards --
    local check = tool.Backpack:GetChildren()
    for i = 1, #check do -- begin of loop
        local Card = check[i] 
        local Value = Card.Keycard.Value -- Crucial Value defining the clearance
        if Value == 5 or Value == 4 or Value == 3 then -- Correct clearances
            bool = false
            print("Valid")
            script.Parent.AccessGranted:play()
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright green")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright green")
            openDoor() 
            wait(1)
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright red")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright red")
            bool = true
        elseif Value == 2 or Value == 1 then -- false clearances
            bool2 = false
            print("Invalid")
            script.Parent.AccessDenied:play()
            wait(1)
            bool2 = true
        end
    end -- end of loop (loops 6 times due to 6 Keycards)
end)

Help would be much appreaciated. I am grateful for just the smallest tip.

1 answer

Log in to vote
0
Answered by 2 years ago

To make the loop stop you can use break

--------------------------------- The first Keycard Reader ------------------------
script.Parent.Parent.KeycardReader1.ProximityPrompt.Triggered:connect(function(tool)
    ------------------------ checking for the value in the keycards --
    local check = tool.Backpack:GetChildren()
    for i = 1, #check do -- begin of loop
        local Card = check[i] 
        local Value = Card.Keycard.Value -- Crucial Value defining the clearance
        if Value == 5 or Value == 4 or Value == 3 then -- correct clearances 
            bool = false
            print("Valid")
            script.Parent.AccessGranted:play()
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright green")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright green")
            openDoor()
            wait(1)
            script.Parent.Parent.Light1.brickColor = BrickColor.new("Bright red")
            script.Parent.Parent.Light2.brickColor = BrickColor.new("Bright red")

            break -- exit the loop
        elseif Value == 2 or Value == 1 then -- false clearances
            bool2 = false
            print ("Invalid")
            script.Parent.AccessDenied:play()
            wait(1)
            break -- exit the loop
        end
    end-- end of loop (loops 6 times due to 6 keycards)
end)
0
Thank you, I didnt know break existed. Attalkiseer 10 — 2y
Ad

Answer this question