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.


function loop()  -- Loop function for menu cinematics
	currentCamera.CFrame = workspace.Cam1.CFrame
	currentCamera.CFrame = workspace.Cam2.CFrame
	currentCamera.CFrame = workspace.Cam3.CFrame

to this

local currentCamera = workspace.CurrentCamera

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

local function CancelTween()
    for i,v in pairs(tweenCam) do

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

23 lines of code.


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,,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam1.CFrame +,0,-100)}), Instance = game.Workspace.Cam1},
    ["cam2"] = {Tween = tweenService:Create(currentCamera,,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam2.CFrame +,0,50)}), Instance = game.Workspace.Cam2},
    ["cam3"] = {Tween = tweenService:Create(currentCamera,,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{CFrame = workspace.Cam3.CFrame +,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