Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

If i were to catch the end of a for loop, would this be right?

Asked by 5 years ago

Hi! The situation is: I'm lerping a model to move to another part The problem is: I can't figure out how to stop the for loop when the alpha (defined as i) of the lerp becomes a certain number.

Here's my code: (Ignore the comments, they aren't important to my problem/question)

local model = script.Parent
local dest = workspace.Solar_System.ShipReDirect1.Position
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 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       
        StartCF = CFrame.new(model.PrimaryPart.Position,reDirect.Value.Position)
    end
end
getRedirect()
local EndCF = StartCF - StartCF.p + dest --keep direction for destination, but use new destination position

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 script.Parent.PrimaryPart.CFrame == reDirect.Value.Position then
            StartCF = CFrame.new(model.PrimaryPart.Position,dest)
            for i = 0,1,0.001 do
                model:SetPrimaryPartCFrame(StartCF:lerp(EndCF,i))
            end
        end
end

I'm still working on the code, but if it's a little confusing i can send a picture of the physical thing and/or the Workspace or more explanation/code on it. Don't hesitate to ask please!

Thank you so much! :)

1 answer

Log in to vote
2
Answered by
MrMedian 160
5 years ago
Edited 5 years ago

Use break

for i = 0, 1, 0.001 do
    model:SetPrimaryPartCFrame(StartCF:lerp(EndCF,i))
    if i == certainNumber then
        break
    end
end
0
Thank you a bunch! Chez_Guy 67 — 5y
Ad

Answer this question