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

How to tell a script to wait if something is complete?

I'm making a door which opens using Vector3 but how do I tell it to not continue moving once it has reached a set position?

LDoor = game.Workspace.OpeningD.LDoor
RDoor = game.Workspace.OpeningD.RDoor
IsRunning = true
function OpenDoor()
	if IsRunning == true then
		IsRunning = false
	while true do
		LDoor.CFrame = LDoor.CFrame - Vector3.new(0,0,0.1)
		RDoor.CFrame = RDoor.CFrame + Vector3.new(0,0,0.1)
		wait(0.01)
			OpenDoor = false
			IsRunning = false
		end
	end
end

script.Parent.ClickDetector.mouseClick:connect(OpenDoor)

2 answers

make an if statement

LDoor = game.Workspace.OpeningD.LDoor
RDoor = game.Workspace.OpeningD.RDoor
IsRunning = true
function OpenDoor()
    if IsRunning == true then
        IsRunning = false
    while true do
	if LDOOR.CFrame ~= CFrame.new(0,0,100) then
        LDoor.CFrame = LDoor.CFrame - Vector3.new(0,0,0.1)
        RDoor.CFrame = RDoor.CFrame + Vector3.new(0,0,0.1)
        wait(0.01)
            OpenDoor = false
            IsRunning = false
        end
end
    end
end

script.Parent.ClickDetector.mouseClick:connect(OpenDoor)

This is fixed script Upper one will not work

LDoor = game.Workspace.OpeningD.LDoor
RDoor = game.Workspace.OpeningD.RDoor

-- StartCFrame = RDoor.CFrame -- Used to the closing function (Delete comment if u want to use this)
EndCFrame = RDoor.CFrame * CFrame.new(0, 0, 5) -- It will end when it will move 5 studs You can change this

IsRunning = false
function OpenDoor()
	if IsRunning == false then
	IsRunning = true
	while isRunning do
		LDoor.CFrame = LDoor.CFrame * CFrame.new(0, 0, 0.1):inverse() -- You cant use - + You can only use * and / so there must be invertion
		RDoor.CFrame = RDoor.CFrame * CFrame.new(0, 0, 0.1)
		wait(0.01)
		 if RDoor.CFrame == EndCFrame then -- When right door reach choosen location it will stop
			IsRunning = false
		end
	end
	--[[ -- If you want there is the closing function (Delete comment if you want to use this)
	isRunning = true
	while isRunning
		LDoor.CFrame = LDoor.CFrame * CFrame.new(0, 0, 0.1)
		RDoor.CFrame = RDoor.CFrame * CFrame.new(0, 0, 0.1):inverse()
		wait(0.01)
		 if RDoor.CFrame == StartCFrame then
			IsRunning = false
		end
	end
	]]
end

script.Parent.ClickDetector.mouseClick:connect(OpenDoor)

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