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

why is my CFrame door not opening and closing properly?

When you click on the clickbox the door should open or close depending on the open value

https://gyazo.com/91eac152b5f7a398c9ed92b9d383d72e here is what is happening instead

I believe it has to do with me changing values too soon or something but that's my only theory and I don't see why that would be the case.

Output shows no errors so the code is technically working

https://gyazo.com/27eab086dff60e2cd9ee5d2423e69dfc - photo of the door model in explorer

debounce = false
open = false

model = script.Parent.Parent.Parent
Door = model.Frame

script.Parent.MouseClick:connect(function(part)
    if debounce == true and open == true then return end
        debounce = true
    
    for x = 0, 15 do
        wait(.05)
    if open == false then
        Door.CFrame = Door.CFrame + Vector3.new(0,0,-.30)
        open = true
		wait(3)
        debounce = false
		end
	end
end)
        
script.Parent.MouseClick:connect(function(part)
    if debounce == true and open == false then return end
        debounce = true
    
    for x = 0, 15 do
        wait(.05)
    if open == true then
        Door.CFrame = Door.CFrame + Vector3.new(0,0,.30)
        open = false
		wait(3)
        debounce = false
		end
	end
end)

Any help is appreciated

1 answer

✓ Accepted answer

If you're having trouble understanding why your code doesn't behave as expected, it's often a good idea to set a breakpoint and step through the code one statement at a time.

Just click next to a line no. and a red ball appears. This is a breakpoint. Now test your game and click on the door.

https://i.imgur.com/P7gqSBc.png

You can now keep clicking Step Into to go through your code one statement at a time.

https://i.imgur.com/ffCwmjH.png

Also set up some variable watches to see what values the variables have at the current time of execution. Especially notice open.

https://i.imgur.com/gcuUcCM.png

On the second or 3rd time through the for loop, you should get something like this:

https://i.imgur.com/z8Ed8EW.png

Notice how open is false, so the if statement is skipped the rest of the iterations.

In other words, the problem is that you're effectively making the loop stop early by setting open to true too early. You can think of the for loop as the "animation" part of the script, so doing checks to see if the animation should run, or changing the "open" state of the door, shouldn't happen there. You want to change the debounce and open states before starting the animation and after it ends.

Like so:

clickBox.ClickDetector.MouseClick:connect(function(part)
    if debounce == true and open == true then return end
    
	if open == false then
		debounce = true
		
	    for x = 0, 15 do
	        wait(.05)	    
			DoorMover.CFrame = DoorMover.CFrame + Vector3.new(0,0,-.30) 
	    end
		
		wait(3)
		debounce = false
		open = true
	end
end)

There's another problem with your code though. What happens if we click twice in a short time?

The first clickdetector listener actually enters the for loop twice, because the first check only returns if BOTH deboune and open are true.But open isn't set to true before the end of the loop + 3 seconds, so the door starts the opening animation twice at the same time, making it move twice as far and twice as fast. If you click it 10 times, it will move 10x.

Another problem is that you're constantly adding a small amount to the CFrame and subtracting it later. This doesn't mean the door will actually end up in the same original position! You'll have to store the original CFrame and them move it back to that whenever the door has finished its closing animation, to reset it properly.

2 comments

0

Still applying everything I learned as I write this, but thank you so much! I never expected such a large, well written, and informative response! Thank you for everything!

0

Also I changed the 'and' to an 'or' in the if debounce == true and open == true line and it fixed the clicking more than once problem. The door is working!!! thank you! :)

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