So I have made a door with this script. So when I click on it the door swings open. Tho I want it to make it so when door is open and I click on it will close again to the same postion, how could I do this?
local Door = script.Parent.Parent local Main = Door.Main local Button = script.Parent local Opened = false Button.ClickDetector.MouseClick:connect(function() if Opened == false then Opened = true for i = 0,3,.1 do Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(math.pi/1),0)) wait() end end end)
Hi.
In this case you should use else
statement.
It is is simple to use:
local Open = false function Door() if Open = false then Open = true for i=0, 3 do Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(math.pi/1),0)) wait(0.1) end else --If Open is not false then it will do the following: for i=0, 3 do Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,-math.rad(math.pi/1),0)--We will be using negative math.rad() in order to close wait(0.1) end Open = false --When it is closed, we must tell the script that it is end end Button.ClickDetector.MouseClick:connect(Door)
Hope this helps.