Improved CloneTrooper1019 Cutscene Script Minor Issue

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
2 Likes

the issue with your script is likely caused by the way you handle the totalcache table. since you’re using v as the key, the problem arises because you’re repeatedly overwriting the same key with the preload table in your tweencam function. this leads to all the keys in totalcache referencing the same preload table, effectively making every entry in totalcache the same as the first entry.

to fix this issue, you need to create a new table for each v key in totalcache. here’s a modified version of your tweencam function that should work correctly:

function tweencam(c1, f1, time, fov, roll, v, reverse)
local prel = cacheframes(c1, f1, time, fov, roll)
if totalcache[v] == nil then
totalcache[v] = prel
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

this change ensures that each v key in totalcache references a unique preload table, preventing the repetition of the first frame sequence.

2 Likes

I’ll give that a shot but v is a number between 1 and the # of data points which is 9 which I’ve printed the values to maintain sanity. So v is 1,2,3,4,5,6,7,8,9. and written directly into the index as globalcache[v] = the preloaded cache. Which I would assume would be the local prel value which is a copy of the results returned from the cacheframes function. But maybe since I used an external table it’s somehow not providing a copy of the table as I would’ve thought… I see what you’re getting at though so I’ll try it out!

1 Like

thank you for the additional context. if v is indeed a number from 1 to 9, and you’re using it as the index for totalcache, then the issue is likely not with the key itself. however, you’re correct that it’s possible that the assignment is not behaving as expected with an external table.

here are a few things you can check:

  1. make sure that the cacheframes function is returning the expected prel table for each v. you can print the contents of prel within the tweencam function to verify this.
  2. double-check that totalcache is not being modified elsewhere in your script. it should only be modified within the tweencam function.
  3. ensure that the data table contains the expected data for each v, and that the indices match what you’re using in tweencam.
  4. confirm that v is being incremented correctly in your loop. it should start from 1 and go up to 9 as you mentioned.

if you’ve checked these aspects and the issue persists, it’s possible that the problem lies in the cacheframes function or the way data is organized in the data table. if you need further assistance, please provide more details about the cacheframes function and the structure of the data table, so i can better diagnose the issue.

1 Like

Try this:

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 preload = {}
local totalcache = {}

local function cacheFrames(c1, f1, time, fov, roll)
    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

local function tweenCam(c1, f1, time, fov, roll, v, reverse)
    local prel = cacheFrames(c1, f1, time, fov, roll)
    totalcache[v] = prel
    local frames = prel[1].frames
    for i = 1, frames do
        Camera.CameraType = prel[i].CameraType
        Camera.CoordinateFrame = prel[i].CoordinateFrame
        Camera.FieldOfView = prel[i].FieldOfView
        Camera:SetRoll(prel[i].roll)
        task.wait()
        if not Enabler then
            break
        end
    end
end

while Enabler do
    for i = 1, #data do
        if not Enabler 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 not Enabler 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

EDIT: When I replied, you marked his solution correct :rofl:… sorry!

1 Like

Yes it was as simple as making the preload table a local variable within the tween cam function!
Now I can see that tabularized the frames into a global cache is the only thing causing bugs now. Because that’s the only real variable. Thanks for your help!

Your very welcome pal’ :heart:
Glad I could help you out

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.