How can i track when a CFrame lerp is finished?


  • Hi! :)

    This is what i want the script to do: I have a for loop with a CFrame lerp in it going to one part, when the lerp is finished, it'll go from the part it went to, to another part then end.

    This is the problem: In the loop, i cant catch when its reached the first part, -or when the lerp finished- then tell the script to go to the other part. Instead of going to the other part, it just stops in place and does nothing. Help!?

    (If you are confused with my question, go to my question before this)

    My code:

    local model = script.Parent
    local dest = workspace.Solar_System.ToChoosePlanet1.Position --tweak this when in real in-game script
    local reDirect = script.ReDirect --tweak this when in real in-game script
    local StartCF = CFrame.new(model.PrimaryPart.Position,dest) --point model to destination
    local EndCF = StartCF - StartCF.p + dest --keep direction for destination, but use new destination position
    
    local function getRedirect()
    	--[[
    		The reDirect is when if a ship is going to another planet and it noclips throught the sun, 
    		it will go to one part to avoid the sun, then when it reaches the redirect part, it heds off
    		to the planet of its choosen destination.
    	--]]
    	if dest == workspace.Solar_System.ToChoosePlanet1.Position then --Put in many more of these if then state.s in real script
    		reDirect.Value = workspace.Solar_System.ShipReDirect4		
    		local StartCF = CFrame.new(model.PrimaryPart.Position,reDirect.Value.Position)
    		local EndCF = StartCF - StartCF.p + reDirect.Value.Position --keep direction for destination, but use new destination position
    	end
    end
    getRedirect()
    
    
    for i = 0,1,0.001 do --adding more 0s infront of the 1 will make the ship travel slower
      wait(0.001)
      model:SetPrimaryPartCFrame(StartCF:lerp(EndCF,i))
    	if dest == workspace.Solar_System.ToChoosePlanet1.Position then --Put in an else if instead of end if its another planet
    		reDirect.Value = workspace.Solar_System.ShipReDirect4
    		dest = reDirect.Value.Position
    		StartCF = CFrame.new(model.PrimaryPart.Position,reDirect.Value.Position)
    		EndCF = StartCF - StartCF.p + reDirect.Value.Position --keep direction for destination, but use new destination position
    	end
    	if i == 1 then
    		dest = workspace.Solar_System.ToChoosePlanet1.Position --tweak this when in real in-game script
    		StartCF = CFrame.new(model.PrimaryPart.Position,dest)
    		EndCF = StartCF - StartCF.p + dest --keep direction for destination, but use new destination position
    		for i = 0,1,0.001 do
    			 wait(0.001)
      		 model:SetPrimaryPartCFrame(StartCF:lerp(EndCF,i))
    		end		
    		break
    	end
    end
    

    That's the code! If you need help or need more information please do not hesitate to ask for it, I will definitely include some more! Thank you so much!


  • @Chez_Guy um just run code after the lerp

  • Global Moderator

    @incapaz Exactly.

    CFrame:lerp() is finished when CFrame:lerp() returns. ¯\_(ツ)_/¯

    However to do something after the whole transition is finished, do so after your for loop.

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.