This door opening code doesn't work sometimes

Door opening code:

local sp = script.Parent
local cd = sp.ClickDetector

local debounce = false
local openable = true

sp.ClickDetector.MouseClick:Connect(function(plr)
	if openable == true then
		if plr.Team == game.Teams.Humans then
			if sp:FindFirstChild("WeldConstraint") then
				if debounce == false then
					debounce = true
					openable = false
					for i, v in pairs(sp:GetChildren()) do
						if v:IsA("WeldConstraint") then
							v.Enabled = false
						end
					end
					sp.Anchored = true
					sp.CFrame = (sp.CFrame*CFrame.Angles(0,math.rad(90),0))
					sp.CFrame = sp.CFrame + (sp.CFrame.LookVector*-1.5) + (sp.CFrame.RightVector*-1.5)
					for i, v in pairs(sp:GetChildren()) do
						if v:IsA("WeldConstraint") then
							v.Enabled = true
						end
					end
					sp.Anchored = false
					wait(0.2)
					openable = true
				else
					openable = false
					for i, v in pairs(sp:GetChildren()) do
						if v:IsA("WeldConstraint") then
							v.Enabled = false
						end
					end
					sp.Anchored = true
					sp.CFrame = sp.CFrame + (sp.CFrame.LookVector*1.5) + (sp.CFrame.RightVector*1.5)
					sp.CFrame = (sp.CFrame*CFrame.Angles(0,math.rad(-90),0))
					for i, v in pairs(sp:GetChildren()) do
						if v:IsA("WeldConstraint") then
							v.Enabled = true
						end
					end
					sp.Anchored = false
					wait(0.2)
					debounce = false
					openable = true
				end
			end
		end
	end
end)

In my game, blocks are unanchored and welded together, so structures can fall. But when people in my game place a door next do a door, it’ll not open sometimes. Although the code for the door CFraming works, there’s just no results. The reverse opening code also runs after you click it again, so it isn’t opening and closing extremely fast.

  • A placed block next to a door will be parented to the door, so that the door can anchor itself, then disable the weld constraints and reverse it instantly after. If I don’t do that, it’ll move your whole entire structure.

  • If a door is placed next to a door, the door placed will keep to itself

1 Like