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 :)
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.
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.