Hi all, I have written this script which opens and closes a door. It fine the first time I press to open (by interacting with a part) and close (by interacting with a gui button) but after that only the opening works correctly but when i try to close for the second, third, and so on, times the angle of the door closing is doubled, tripled, etc.

I'm not sure what the problem is, is the function being called twice? have I put something inside of something I shouldn't have?

Any help would be much appreciated, thank you!

--Right click to interact with objects (click the lock to open the lid)
local door = script.Parent.Parent.Parent.top.bars
local door1 = script.Parent.Parent.Parent.top.wood
local clicked = script.Parent.ClickDetector
	local canClick = true
	
local swing = 0.5 --- the angle to open the door by

-- locate the hinge on the door
local hingeBefore = door.CFrame * CFrame.new(0, (-1.625/2), (4.476/2))

-- relative to the hinge, where is the door?
local relative = hingeBefore:toObjectSpace(door.CFrame)

-- twist the hinge
local hingeAfter = hingeBefore * CFrame.Angles(0, 0, 0)

-- re-attach the door to the new "hinge"
door.CFrame = hingeAfter:toWorldSpace(relative)



-- swing the door by `angle` degrees
function swingDoor(angle)
    -- locate the hinge on the door
    local hingeBefore = door.CFrame * CFrame.new(0, (-1.275/2), (3.725/2))

    -- relative to the hinge, where is the door?
    local relative = hingeBefore:toObjectSpace(door.CFrame)

    -- twist the hinge
    -- CONVERT angle *from* degrees *to* radians here
    local hingeAfter = hingeBefore * CFrame.Angles(math.rad(angle), 0, 0)

    -- re-attach the door to the new "hinge"
    door.CFrame = hingeAfter:toWorldSpace(relative)
	door1.CFrame = hingeAfter:toWorldSpace(relative)
end

clicked.MouseClick:connect(function(playerClicked)
	local gui = playerClicked.PlayerGui.home_bank
	if canClick == true
		then canClick = false
		gui.Enabled = true
		for i = 1, 100 do
        	swingDoor(swing)
        	wait()
		end
		gui.Frame.exit.MouseButton1Click:connect(function()
		gui.Enabled = false
		for i = 1, 100 do
			swingDoor(-swing)
	    	wait()
		end
		canClick = true	
		end)
		end		
end)	

1 comment

0

Please ignore some of the comments I've just noticed some are outdated and incorrect :)