I've been on this script for I kid you not 8 hours plus trying to find help online but nobody has given me any helpful tips so I come here asking for aid, somebody said to use a bool value so I did but the lock and unlock code at line - 39 only unlocks the door, but can't be relocked again.
-- Needs to be locked and unlockable. So far it can only be Unlocked. local Hinge = script.Parent.PrimaryPart local opened = false local door = script.Parent.DoorClick local clickDetector = door.ClickDetector local doorunlocksound = script.Parent.DoorClick.DoorUnlock local doorlocksound = script.Parent.DoorClick.DoorLocked local doorlock = script.Parent.Lock function Open() if opened == false then opened = true for i = 1, 10 do script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(9), 0)) for _,v in pairs(script.Parent.Parent:GetDescendants()) do if v:IsA("BasePart") then v.CanCollide = false end end wait() end else opened = false for i = 1, 10 do script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-9), 0)) wait() for _,v in pairs(script.Parent.Parent:GetDescendants()) do if v:IsA("BasePart") then v.CanCollide = true end end end end end script.Parent.DoorClick.ClickDetector.MouseClick:Connect(Open) -- the issue vvv door.Touched:Connect(function(k) if k.Parent.Name == "Master Key" then doorlocksound:Play() doorlock.Value = true clickDetector.MaxActivationDistance = 0 else doorunlocksound:Play() doorlock.Value = false clickDetector.MaxActivationDistance = 12 end end)
I think I see the issue here.
local Hinge = script.Parent.PrimaryPart local opened = false local door = script.Parent.DoorClick local clickDetector = door.ClickDetector local doorunlocksound = script.Parent.DoorClick.DoorUnlock local doorlocksound = script.Parent.DoorClick.DoorLocked local locked = false local touchcooldown = false function Open() if opened == false then opened = true for i = 1, 10 do script.Parent:SetPrimaryPartCFrame(Hinge.CFrame * CFrame.Angles(0, math.rad(9), 0)) for _, v in pairs(script.Parent.Parent:GetDescendants()) do if v:IsA("BasePart") then v.CanCollide = false end end wait() end else opened = false for i = 1, 10 do script.Parent:SetPrimaryPartCFrame(Hinge.CFrame * CFrame.Angles(0, math.rad(-9), 0)) wait() for _, v in pairs(script.Parent.Parent:GetDescendants()) do if v:IsA("BasePart") then v.CanCollide = true end end end end end script.Parent.DoorClick.ClickDetector.MouseClick:Connect(Open) -- the issue vvv door.Touched:Connect(function(k) if k.Parent.Name == "Master Key" and locked == false and touchcooldown == false then --//Changed it so it checks if its locked or not before running the line. touchcooldown = true doorlocksound:Play() locked = true clickDetector.MaxActivationDistance = 0 wait(2) -- change to whatever touchcooldown = false elseif k.Parent.Name == "Master Key" and locked == true and touchcooldown == false then --//Changed it so it checks if its locked or not before running the line. touchcooldown = true doorunlocksound:Play() doorlock.Value = false clickDetector.MaxActivationDistance = 12 wait(2) -- change to whatever touchcooldown = false end end)
I think the issue lies around the fact that you are not checking whether or not the door is locked before you try locking/unlocking it. I made it so if the key touches the door it checks to make sure the key is touching it and I also checked if the door was locked. If the door was locked then it would play the unlock vice versa!
I also added a cooldown so the door doesnt lock and unlock a bunch of times when you move the key into it.