Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Adding a lock function on my door?

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

✓ Accepted answer

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)

1 comment

0

Dude thank you so much! It worked perfect! Thank alot!

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 ;(

1 comment

0

MouseButton1down is not an event of a ClickDetector, but rather, a GUI Button.

This discussion is preserved as it was. New answers, comments, and votes are closed.