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

Door Opens and Closes at the same time?

Asked by
glinary 19
7 years ago
Edited 7 years ago

Hello, I'm having a problem where I'm making a glass door. I want it to open when clicked, then it closes on the second click. I already have a ClickDetector.

Yeah, it detects it as opened so it closes. It also detects it as closed so it opens.

However, the detection is happening all too fast that it opens and closes, all at the same time.

"wait" won't work. If I make it wait, then it just closes again after a while on its own.

-- Scripted by Glinary
-- left and right glass of Cabinet 1 open and close

--object variables
local Left = script.Parent.Left

-- checks if it's closed
local LeftCloseCheck = {Left.CFrame == CFrame.new(-193.67, 5.72, -7.2), Left.Rotation == Vector3.new(0,0,0)}

-- CFrames and Rotations for closed and opened situations
local LeftOpenCFrame = CFrame.new(-194.88, 5.83, -5.82)
local LeftOpenRotation = Vector3.new(0, -90, 0)
local LeftCloseCFrame = CFrame.new(-193.67, 5.72, -7.2)
local LeftCloseRotation = Vector3.new(0,0,0)

-- checks if it's opened
local LeftOpenCheck = {Left == LeftOpenCFrame, Left == LeftOpenRotation}

-- when object is clicked 
Left.ClickDetector.MouseClick:connect(function(player)

--if its closed then it opens
    if LeftCloseCheck then
        print "im closed so let's open"
        Left.CanCollide = false
        Left.CFrame = LeftOpenCFrame
        Left.Rotation = LeftOpenRotation
    end

-- if its opened then it closes
        if LeftOpenCheck then
        print "i'm opened so let's close"
        Left.CFrame = LeftCloseCFrame
        Left.Rotation = LeftCloseRotation
    end
end)

This is the output after ONE click on the brick.

im closed so let's open
i'm opened so let's close

Thanks for your help and taking the time to read :)

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

I believe you should change the script into something like this:

-- Scripted by Glinary
-- left and right glass of Cabinet 1 open and close

--object variables
local Left = script.Parent.Left

-- checks if it's closed
local LeftCloseCheck = {Left.CFrame == CFrame.new(-193.67, 5.72, -7.2), Left.Rotation == Vector3.new(0,0,0)}

-- CFrames and Rotations for closed and opened situations
local LeftOpenCFrame = CFrame.new(-194.88, 5.83, -5.82)
local LeftOpenRotation = Vector3.new(0, -90, 0)
local LeftCloseCFrame = CFrame.new(-193.67, 5.72, -7.2)
local LeftCloseRotation = Vector3.new(0,0,0)

-- checks if it's opened
local LeftOpenCheck = {Left == LeftOpenCFrame, Left == LeftOpenRotation}

-- when object is clicked 
Left.ClickDetector.MouseClick:connect(function(player)

--if its closed then it opens
    if LeftCloseCheck then
        print "im closed so let's open"
        Left.CanCollide = false
        Left.CFrame = LeftOpenCFrame
        Left.Rotation = LeftOpenRotation
-- if its opened then it closes
        elseif LeftOpenCheck then
        print "i'm opened so let's close"
        Left.CFrame = LeftCloseCFrame
        Left.Rotation = LeftCloseRotation
    end
end)

I changed the if statement for the check open to elseif the reason behind is because once u click it, it will change to close/open and the script will register the click and immediately notice the change if u use 2 if statements for checking stuff like this.

0
You posted the exact same code I posted. Typo? Thanks though. I completely forgot about the "elseif" I'll try to fix it with that and see if it works glinary 19 — 7y
0
Opps AdamFunMaker 35 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

I think that the problem is that you put both statements inside the same function, so when it is run the door is initially closed and it opens, but then the following block of code detects that the door is open and closes it. You could just add an else statement because if the door isn't open then it must be closed.

To fix this I would recommend doing something like this:

Left.ClickDetector.MouseClick:connect(function(player)
    if LeftCloseCheck then
        print "im closed so let's open"
        Left.CanCollide = false
        Left.CFrame = LeftOpenCFrame
        Left.Rotation = LeftOpenRotation
else
    print "i'm opened so let's close"
        Left.CFrame = LeftCloseCFrame
        Left.Rotation = LeftCloseRotation
    Left.CanCollide = true
    end
end)

I hope this helps.

0
In the else you aren't setting the door back to CanCollide = true MrLonely1221 701 — 7y
0
Thanks. Boogieboon 30 — 7y
0
Thanks for the help! However, it still didn't work XD. I've already realized that's the problem and tried the else statement, but it won't fix. Still, thanks a lot :) glinary 19 — 7y
0
Maybe try adding a wait inside each statement. Boogieboon 30 — 7y
0
I've tried the wait already as I said on my question. Sadly, it won't work :( I'm getting an idea from you guys tho. thx glinary 19 — 7y

Answer this question