Review/feedback on CameraSystem, can I improve?

Hello! I am currently making a fnaf fangame, and obviously, I have a camera system.
Please read the whole post to understand everything
Please take note this post is a bit long

Context

But for some context:
I have many things in the camera system (first floor, second floor, vent map).
Basically first floor and second have, well cameras where you can see what’s happening outside the player’s office. The vent map just gives you 2 maps, UIs, (of both floors) and the vent system, so if an enemy is in one of them, they will be “tracked”

Before, on each camera button, I had individual local scripts for getting the CameraSubject. Now I am remaking lots of codes, then putting them in one script and asking the forum how could I improve. Well here I am!

I remade the camera scripts for the second floor, there are 2 cameras on it, cam09 and cam10

Before I remaded my changing camera code, cameras wouldn’t save, meaning let’s say, on the first floor, you are on cam03, then close them and open them back. The CameraSubject will be the first one, the default one. And now I added that!

Now the remake is succesfull! But just to be sure I am asking the forum for anything that I can improve.

Now please be aware that the remade code will also be made for the first floor.

The code

So for avoiding any confusion, here is the layout of the second floor
image
(The first floor is the same, Parent is an ImageLabel, buttons are TextButtons)
Now the code that changes cameras in between: (local script)

local cam = workspace.CurrentCamera

--Current Camera

local SFMap = script.Parent

--Second Floor Map

local SFFolder = game.Workspace.Cameras.SecondFloor

local Cam09 = SFFolder.CamPart09

local Cam10 = SFFolder.CamPart10

local function ChangingCam(Camera)

cam.CameraSubject = Camera

end

SFMap.Cam09.MouseButton1Click:Connect(function()

ChangingCam(Cam09)

end)

SFMap.Cam10.MouseButton1Click:Connect(function()

ChangingCam(Cam10)

end)

Now some people might be wondering how we remember which camera was choosen last, so here’s a layout:

image
The Value Current is a NumberValue.
Now for the code for changing the NumberValue.Value: (script)

local Current = game.Workspace.CurrentCam.SecondFloor.Current

local cam = workspace.CurrentCamera

--Current Camera

local SFMap = script.Parent

--Second Floor Map

local SFFolder = game.Workspace.Cameras.SecondFloor

local Cam09 = SFFolder.CamPart09

local Cam10 = SFFolder.CamPart10

function ChangeVal2(Value)

Current.Value = Value

print(Current.Value)

end

SFMap.Cam09.MouseButton1Click:Connect(function()

ChangeVal2(09)

end)

SFMap.Cam10.MouseButton1Click:Connect(function()

ChangeVal2(10)

end)

Now the code for when we go to the second floor and remembering which camera was last. (Default is 09): (script)

    local cam = workspace.CurrentCamera

    local Current = game.Workspace.CurrentCam.SecondFloor.Current

    script.Parent.MouseButton1Down:connect(function()
    	--cam.CameraType = Enum.CameraType.Attach
    	if game.Workspace.CurrentCam.SecondFloor.Current.Value == 09 then
    		cam.CameraSubject = game.Workspace.Cameras.SecondFloor.CamPart09

    	elseif game.Workspace.CurrentCam.SecondFloor.Current.Value == 10 then
    		cam.CameraSubject = game.Workspace.Cameras.SecondFloor.CamPart10
    	
    end
	
	

end)

Is there any ways to improve my code?
If there’s any confusion or uncertainty, please tell me! I hope I didn’t forget anything or wasn’t clear enough.
Sorry if this was a bit long.
Anyways, thanks for reading

Instead of saving the last camera to a value I would recommend going for a OOP approach, refer to ModuleScripts.

I would also suggest doing a for loop rather than a bunch of if statements to get and set the camera subject.

1 Like

Thanks! I am a bit new to scripting but I know well. Can you link me to something about OOP?

1 Like

That’s okay, we all start somewhere, and sure!

https://www.lua.org/pil/16.html
https://education.roblox.com/en-us/resources/intro-to-module-scripts

1 Like

I’ll comeback to you with what I will do. As of right now I have school, at lunch I will be available to develop.

So just before I start, let’s say we make our local script for changing CameraSubject, since scripts can’t do that.
Our ModuleScript is in the Vent Map, so we have the variables there. So we should know which CameraSubject we’re choosing and choose.

So when we go to the second floor, we just do a require to ask which should we choose.
I hope I am doing something good.

I’ve never used a module script before, so yeah. Might made some errors in my comments.

ModuleScripts only need to be required and defined once.

i.e

local CameraManager = require(<path>)

Now say in your module script you have a variable, for example

CameraManager.CameraSubject = nil -- I would set a starting subject here

Upon requiring the ModuleScript in your LocalScript, this variable can be changed. Note that upon doing so it doesn’t network to the server, if you do need it on the server you could use a RemoteEvent for example, and then store it relative to the player.

For changing the above variable all you need to do is

CameraManager.CameraSubject = <the camera subject>

Now you can easily derive and set the camera back to the one the player was last on, for example by doing

camera.CameraSubject = CameraManager.CameraSubject -- Assuming you've already defined the camera earlier in the code
1 Like

Thank you for explaining how module scripts work.

1 Like

You’re welcome, I’m glad I could help!

1 Like