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

How to stop a camera lerp while its lerping?

Asked by 5 years ago
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local looping = false

--Camera Parts
local camerapart1 = workspace.CameraParts.CameraPart1
local camerapart2 = workspace.CameraParts.CameraPart2
local camerapart3 = workspace.CameraParts.CameraPart3
local camerapart4 = workspace.CameraParts.CameraPart4

local position = camerapart1
repeat wait()
    camera.CameraType = Enum.CameraType.Scriptable
    looping = true
until camera.CameraType == Enum.CameraType.Scriptable
--
--camera.CFrame = workspace.CameraPart.CFrame

script.Parent.Play.MouseButton1Click:Connect(function()
    looping = false
    camera.CameraType = Enum.CameraType.Custom
    for i = 0, 1, .01 do
        camera.CFrame = position.CFrame:Lerp(camera.CFrame,i)
        game:GetService("RunService").RenderStepped:Wait()
    end
    for i = 2, 0, -.1 do
        wait()
        game.Lighting.Blur.Size = i
    end
end)

while wait() do
    if looping == true then
        looping = true
        for i = 0, 1, .01 do
            camera.CFrame = camerapart1.CFrame:Lerp(camerapart2.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart2
        for i = 0, 1, .01 do
            camera.CFrame = camerapart2.CFrame:Lerp(camerapart3.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart3
        for i = 0, 1, .01 do
            camera.CFrame = camerapart3.CFrame:Lerp(camerapart4.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart4
        for i = 0, 1, .01 do
            camera.CFrame = camerapart4.CFrame:Lerp(camerapart1.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart1
    end
end

when I press the play button, the lerp glitches between the block and the player camera. how do I circumvent this issue?

1 answer

Log in to vote
0
Answered by 5 years ago

So, you are checking that looping is true while in the while loop. But you aren't checking while in the for loop that's actually moving the camera.

An easy fix for this is to just add checks in there between each point, then break out if you're done looping.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local looping = false

--Camera Parts
local camerapart1 = workspace.CameraParts.CameraPart1
local camerapart2 = workspace.CameraParts.CameraPart2
local camerapart3 = workspace.CameraParts.CameraPart3
local camerapart4 = workspace.CameraParts.CameraPart4

local position = camerapart1
repeat wait()
    camera.CameraType = Enum.CameraType.Scriptable
    looping = true
until camera.CameraType == Enum.CameraType.Scriptable
--
--camera.CFrame = workspace.CameraPart.CFrame

script.Parent.Play.MouseButton1Click:Connect(function()
    looping = false
    camera.CameraType = Enum.CameraType.Custom
    for i = 0, 1, .01 do
        camera.CFrame = position.CFrame:Lerp(camera.CFrame,i)
        game:GetService("RunService").RenderStepped:Wait()
    end
    for i = 2, 0, -.1 do
        wait()
        game.Lighting.Blur.Size = i
    end
end)

while wait() do
    if looping == true then
        for i = 0, 1, .01 do
                        if looping == false then break; end;
            camera.CFrame = camerapart1.CFrame:Lerp(camerapart2.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart2
        for i = 0, 1, .01 do
                        if looping == false then break; end;
            camera.CFrame = camerapart2.CFrame:Lerp(camerapart3.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart3
        for i = 0, 1, .01 do
                        if looping == false then break; end;
            camera.CFrame = camerapart3.CFrame:Lerp(camerapart4.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart4
        for i = 0, 1, .01 do
                        if looping == false then break; end;
            camera.CFrame = camerapart4.CFrame:Lerp(camerapart1.CFrame,i)
            game:GetService("RunService").RenderStepped:Wait()
        end
        position = camerapart1
    end
end

If you have any more questions feel free to ask, I'm more than happy to help. If this answered your question don't forget to mark it as the solution.

Ad

Answer this question