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

Open multiple doors with ONE script?

Asked by
Nidoxs 190
8 years ago

I have 3 doors and soon will have more but how do I open all doors with 1 script WITH A DEBOUNCE that WONT stop other doors from opening other than the one that has been pressed. Now I have made this script which DOES indeed work and opens all doors in the game from 1 script however you can multiple-click doors which I do not want to happen so how do I do it? Code is here:

--variables--
local debounce = false

local Doors = game.Workspace.Doors:GetChildren()

for i, v in pairs(Doors)do 
    function open()
    local Door1 = v.Door1
    local Door2 = v.Door2
    local Sound = v.Frame.OpenSound 
    Sound:Play()
    Sound.Pitch = 1
        for i = 1, 30 do 
            Door1.CFrame = Door1.CFrame - Vector3.new(.1,0,0)
            Door2.CFrame = Door2.CFrame + Vector3.new(.1,0,0)
            wait(.01)
        end
        wait(.5)
        Sound:Play()
        Sound.Pitch = .7
        for i = 1, 30 do 
            Door1.CFrame = Door1.CFrame + Vector3.new(.1,0,0)
            Door2.CFrame = Door2.CFrame - Vector3.new(.1,0,0)
            wait(.01)
        end
    end
    v.Panel1.Button.ClickDetector.MouseClick:connect(open)
    v.Panel2.Button.ClickDetector.MouseClick:connect(open)
end


1 answer

Log in to vote
0
Answered by 8 years ago

I assume you want something like this?

--variables--
local Doors = game.Workspace.Doors:GetChildren()

for i, v in pairs(Doors) do 
    local debounce = true -- Surprisingly this will actually work, because it's local to this scope. Next loop, this same line will create a new local variable, local to the next loop. Here is live proof of that: https://repl.it/CADc

    local function open()
        -- Your original script didn't seem to have any debounce, but it's should be just same debounce logic. Most important thing is the local debounce variable.
        if debounce then
            debounce = false

            local Door1 = v.Door1
            local Door2 = v.Door2
            local Sound = v.Frame.OpenSound 
            Sound:Play()
            Sound.Pitch = 1
            for i = 1, 30 do 
                Door1.CFrame = Door1.CFrame - Vector3.new(.1,0,0)
                Door2.CFrame = Door2.CFrame + Vector3.new(.1,0,0)
                wait(.01)
            end
            wait(.5)
            Sound:Play()
            Sound.Pitch = .7
            for i = 1, 30 do 
                Door1.CFrame = Door1.CFrame + Vector3.new(.1,0,0)
                Door2.CFrame = Door2.CFrame - Vector3.new(.1,0,0)
                wait(.01)
            end

            debounce = true
        end
    end

    v.Panel1.Button.ClickDetector.MouseClick:connect(open)
    v.Panel2.Button.ClickDetector.MouseClick:connect(open)
end
Ad

Answer this question