How can I improve this script?

Hi, how can I improve this script performance wise? I know it is really messy.
** Here’s a preview of what the code does **

EDIT: deleted script since someone helped me fix it.

1 Like

i think you should find some way to reduce the script word like use module

Yeah I was going to do that anyway but I meant more like is there any ways of making it more efficient? How would you have done the cinematic loop?

I ended up using modules to optimise the script as much as possible but I am still looking for a way to make the loop more efficient if there is any.

Went from 72 lines of code to 41.

You could turn it into a dictionary, and use a for loop to loop through the table; and execute the functions along with the yield delay before switching to another camera.

Will reduce the amount of messy code and lines down by alot.

example:

function loop()  -- Loop function for menu cinematics
	currentCamera.CFrame = workspace.Cam1.CFrame
	tweenCam.cam1:Play()
	tweenCam.cam1.Completed:Wait()
	currentCamera.CFrame = workspace.Cam2.CFrame
	tweenCam.cam2:Play()
	tweenCam.cam2.Completed:Wait()
	currentCamera.CFrame = workspace.Cam3.CFrame
	tweenCam.cam3:Play()
	tweenCam.cam3.Completed:Wait()
end

to this

local currentCamera = workspace.CurrentCamera

local tweenCam = {
    ["cam1"] = {Tween = tweenService:Create(currentCamera,TweenInfo.new(5,Enum.EasingStyle.Quad,En.EasingDirection.In,0,false,0),{CFrame = workspace.Cam1.CFrame + Vector3.new(0,0,-100)}), Instance = game.Workspace.Cam1};
    
    ["cam2"] = {Tween = tweenService:Create(currentCamera,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam2.CFrame + Vector3.new(0,0,50)}), Instance = game.Workspace.Cam2};
    
    ["cam3"] = {Tween = tweenService:Create(currentCamera,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam3.CFrame + Vector3.new(0,0,50)})}, Instance = game.Workspace.Cam3};
}

local function CancelTween()
    for i,v in pairs(tweenCam) do
        v.Tween:Cancel()
    end
end

function loop()
    for i,v in pairs(tweenCam) do
        currentcamera.CFrame = v.Instance.CFrame
        v.Tween:Play()
        v.Tween.Completed:Wait() -- after this, it will continue on the next cam in the table.
    end
end

23 lines of code.

2 Likes

Hi, this is my first time working with dictionnaries. This part contains errors; "Syntax Error: (19,1) Expected identifier when parsing expression, got '}"

I tried switching it to;

local tweenCam = {
    ["cam1"] = {Tween = tweenService:Create(currentCamera,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam1.CFrame + Vector3.new(0,0,-100)}), Instance = game.Workspace.Cam1},
    
    ["cam2"] = {Tween = tweenService:Create(currentCamera,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam2.CFrame + Vector3.new(0,0,50)}), Instance = game.Workspace.Cam2},
    
    ["cam3"] = {Tween = tweenService:Create(currentCamera,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam3.CFrame + Vector3.new(0,0,50)})}, Instance = game.Workspace.Cam3}

Which fixed the error but the console now outputs: "attempt to call a nil value"

EDIT: nvm, i look into dictionnary and fixed it myself, thank you for telling me about dictionnaries

1 Like