Elevator Door movement loop breaking

Hey, I’ve been trying to script an elevator for the past few hours but one of my functions for the elevator doors keeps breaking, I’ve attempting to change the way the variables are set up a few different times and I’m just stuck. When the function is called, doort2 moves its width in the correct amount like it is supposed too, however immediately after doort1 ends out moving in a direction indefinitely without the loop breaking. How should I go about fixing this? Here is the function:

local function OpenDoorT()
	local moved = 0
	repeat
		doort2.CFrame = doort2.CFrame:ToWorldSpace(CFrame.new(0,0,-0.1))
		moved = moved + 0.1
		wait()
	until moved == 2.8
	local moved2 = 0
	wait(0.5)
	while moved2 <= 3.3 do
		doort2.CFrame = doort2.CFrame:ToWorldSpace(CFrame.new(0,0,-0.1))		
		doort1.CFrame = doort1.CFrame:ToWorldSpace(CFrame.new(0,0,-0.1))

		moved2 = moved2 + 0.1
		wait()
	end
	moved2 = 0
end

Decimals are not precise. Example, 0.1+0.1+0.1 is not 0.3, instead it is 0.30000000000000004. There are many videos about why this is happening.

This means that comparing equality of decimals is almost impossible. In your case, you should use moved >= 2.8 because like I said, decimals are not precise and may lead to your code breaking.

An alternative would be to use integers, as this imprecision only happens on decimals.

moved = moved + 1
until moved = 28
1 Like