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

Why does my gate open but not close?

Asked by 4 years ago
Edited 4 years ago

Hello, I want my gate to open to a certain person with a button click and to close again after another click. It opens, but it doesn't close. Thanks for your help! PinoRisi03

Here's my code:

local opened = false
local Door = script.Parent.Parent.Parent.Door


local AllowedPlayers = {402603503, 1} --- allowed Players

script.Parent.MouseClick:Connect(function(Player)
    local FoundInTable = false
    for _, userid in pairs(AllowedPlayers) do
        if Player.UserId == userid and FoundInTable == false then
            FoundInTable = true
        end
    end
    if FoundInTable == true then
        if opened == false then
        opened = true
        for i = 1,50 do
            Door.CFrame = Door.CFrame + Vector3.new(0,0,0.22)
            wait()
        end
    end
    else
        opened = false
        for i = 1,50 do
            Door.CFrame = Door.CFrame - Vector3.new(0,0,0.22)
            wait()
        end
    end
end
end)
end

1 answer

Log in to vote
1
Answered by
Soban06 410 Moderation Voter
4 years ago

I think you have confused the if statements. Try this. It may work:

local opened = false
local Door = script.Parent.Parent.Parent.Door


local AllowedPlayers = {402603503, 1} --- allowed Players

script.Parent.MouseClick:Connect(function(Player)
    local FoundInTable = false
    for _, userid in pairs(AllowedPlayers) do
        if Player.UserId == userid and FoundInTable == false then
            FoundInTable = true
        end
    end
    if FoundInTable == true then
        if opened == false then
        opened = true
        for i = 1,50 do
            Door.CFrame = Door.CFrame + Vector3.new(0,0,0.22)
            wait()
        end

    else
        opened = false
        for i = 1,50 do
            Door.CFrame = Door.CFrame - Vector3.new(0,0,0.22)
            wait()
        end
    end

    end
end
end)
end

Ad

Answer this question