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

Adding a lock function on my door?

Asked by 6 years ago

So I have made an entire door script with CFrame that works just fine. But the important thing I need is a lock. I want to do it so that when I press a lock with a clickdetector the script detects that it's locked and you can no longer open the door until the lock is pressed again.

I have no idea how to do this, I tried a little but nothing works, please help me out!

Door script:

local Door = script.Parent.Parent
local Button = script.Parent
local Debounce = true
local Opened = false
local locked = false
local Lock = Door.Lockbutton
local ClickDetector2 = Lock.ClickDetector


Button.ClickDetector.MouseClick:connect(function()
    if not Opened and Debounce then
        Opened = true
        Debounce = false
        for i = 0,2,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(math.pi/-0.7),0))
            wait()
        end
        Debounce = true
    end
end)
wait(3)
Button.ClickDetector.MouseClick:connect(function()
    if Opened and Debounce then
        Opened = false
        Debounce = false
        for i = 0,2,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(math.pi/0.7),0))
            wait()
        end
        Debounce = true
    end
end)

2 answers

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 years ago

This should work.

local Door = script.Parent.Parent
local Button = script.Parent
local Debounce = true
local Opened = false
local locked = false
local Lock = Door.Lockbutton
local ClickDetector2 = Lock.ClickDetector
local angle = 90


Button.ClickDetector.MouseClick:Connect(function()
    if Debounce and not locked then
        Opened = not Opened
        Debounce = false
        for i = 0,2,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad((Opened and -(angle/20)) or (angle/20)),0))
            wait()
        end
        Debounce = true
    end
end)

ClickDetector2.MouseClick:Connect(function()
    locked = not locked
end)
0
Dude thank you so much! It worked perfect! Thank alot! ItsRickGrimes 20 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
local locked = 

mousedetector.MouseButton1down:connect(function(player)
if player.Backpack:FindFirstChild("Key") then
    locked = false
        --open door
        else
        locked = true
        --door stays closed
end
end)

hopefully u get the concept my studio is crashing on me so ;(

0
MouseButton1down is not an event of a ClickDetector, but rather, a GUI Button. UgOsMiLy 1074 — 6y

Answer this question