So I wrote this code to optimize this cutscene script from a old plugin that creates cutscenes. First I did this by creating a frame buffer that preload the math for the frames before executing them. Then I wanted to play the cache back using the totalcache table with the index number “v” as the key. But it appears to just repeat the first frame sequence and I cannot figure out why. I commented out the line which causes the bug. Otherwise this works perfectly in optimizing the script but it can be further improved by utilizing the global cache. IT should work since all the variable are tabularized into an array but it just doesn’t work when you want to play back the total cache
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local data = HttpService:JSONDecode(script.CutsceneData.Value)
local localPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Enabler = true
local prel = nil
local preload = {}
local totalcache={}
local function cacheFrames(c1, f1, time, fov, roll)
local simcam = Camera
local camCFrame,camFocus,camFOV,camRoll,frames = Camera.CoordinateFrame, Camera.Focus, Camera.FieldOfView, Camera:GetRoll(), time/0.015
for i = 1,frames do
preload[i]=
{
CameraType = Enum.CameraType.Scriptable,
CoordinateFrame = CFrame.new(camCFrame.p:Lerp(c1.p,i/frames),camFocus.p:Lerp(f1.p,i/frames)),
FieldOfView = (camFOV+(fov-camFOV)*(i*(1/frames))),
roll = camRoll+(roll-camRoll)*(i*(1/frames)),
frames = frames
}
end
return preload
end
function tweenCam(c1, f1, time, fov, roll,v,reverse)
--if totalcache[v]==nil then
local prel= cacheFrames(c1, f1, time, fov, roll)
totalcache[v] = prel--problem line
--else end
local frames=totalcache[v][1].frames
for i = 1,frames do
Camera.CameraType = totalcache[v][i].CameraType
Camera.CoordinateFrame = totalcache[v][i].CoordinateFrame
Camera.FieldOfView = totalcache[v][i].FieldOfView
Camera:SetRoll(totalcache[v][i].roll)
task.wait()
if Enabler == false then
break
end
end
end
while Enabler do
for i = 1,#data do
if Enabler == false then
break
end
tweenCam(CFrame.new(unpack(data[i].c1)),CFrame.new(unpack(data[i].f1)), data[i].step, data[i].FOV, data[i].Roll,i,false)
end
local i = #data
for v = 1,#data do
i = i - 1
if Enabler == false then
break
end
if i > 1 then
tweenCam(CFrame.new(unpack(data[i].c1)),CFrame.new(unpack(data[i].f1)), data[i].step, data[i].FOV, data[i].Roll,i,false)
end
end
end
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = localPlayer.Character.Humanoid