Feedback for my camera moving system

Hello friends! I’m currently working on a camera moving system for my fnaf 4 inspired game!
I think I have tackled most major problems, but have I done them efficiently through my code is the question :wink:

Essentially, when you click on a button, it should disable the other buttons and tween the camera to the part. It will then give it some time, give a prompt to go back to the original position and then you can repeat.

You can test this code at the start of my game if you feel so!
https://www.roblox.com/games/8491443345/WIP#!/game-instances

function LoadRoom()
	RoomAmbience:Play()
	RoomAmbience.Looped = true
	wait(cutsceneTime)
	camera.CFrame = Maincamera.CFrame
	
	-- Load camera Ui's
	local Screen = Instance.new("ScreenGui")
	Screen.Parent = PlayerGui
	local ButtonLeft = Instance.new("TextButton")
	ButtonLeft.Parent = Screen
	ButtonLeft.Size = UDim2.new(0.040,0,0.120,0)
	ButtonLeft.AnchorPoint = Vector2.new(0, 0.5)
	ButtonLeft.Position = UDim2.new(0, 0, 0.5, 0)
	ButtonLeft.Visible = true
	local ButtonRight = Instance.new("TextButton")
	ButtonRight.Parent = Screen
	ButtonRight.Size = UDim2.new(0.040,0,0.120,0)
	ButtonRight.AnchorPoint = Vector2.new(1, 0.5)
	ButtonRight.Position = UDim2.new(1, 0, 0.5, 0)
	ButtonRight.Visible = true
	local ButtonBack = Instance.new("TextButton")
	ButtonBack.Parent = Screen
	ButtonBack.Size = UDim2.new(0.040,0,0.120,0)
	ButtonBack.AnchorPoint = Vector2.new(0.5, 1)
	ButtonBack.Position = UDim2.new(0.5, 0, 1, 0)
	ButtonBack.Visible = true
	
	
	-- Events
	ButtonLeft.MouseButton1Click:Connect(function()
		ButtonLeft.Visible = false
		ButtonRight.Visible = false
		ButtonBack.Visible = false
		local tweenInfo = TweenInfo.new(
			3,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local TweenToLeftDoor = TweenService:Create(camera, tweenInfo, {CFrame = LeftCamera.CFrame})
		TweenToLeftDoor:Play()
		PositionLeftDoor:FireServer()
		task.wait(3)
		ButtonBack.Visible = true
	end)
	
	ButtonRight.MouseButton1Click:Connect(function()
		ButtonRight.Visible = false
		ButtonLeft.Visible = false
		ButtonBack.Visible = false
		local tweenInfo = TweenInfo.new(
			3,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local TweenToRightDoor = TweenService:Create(camera, tweenInfo, {CFrame = RightCamera.CFrame})
		TweenToRightDoor:Play()
		PositionRightDoor:FireServer()
		task.wait(3)
		ButtonBack.Visible = true
	end)
	
	
	
	ButtonBack.MouseButton1Click:Connect(function()		
		
		if ButtonLeft.Visible == false or ButtonRight.Visible == false then
			ButtonLeft.Visible = false
			ButtonRight.Visible = false
			ButtonBack.Visible = false
		end
		
		
		
		
		local tweenInfo = TweenInfo.new(
			3,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local TweenBack = TweenService:Create(camera, tweenInfo, {CFrame = Maincamera.CFrame})
		TweenBack:Play()
		task.wait(3)
		ButtonLeft.Visible = true
		ButtonRight.Visible = true
	end)
	
	
end



2 Likes

Hi, I took a look at your code. Nothing really bothers me, but you’ll want to get some better feedback from more advanced developers.

It’s clean, easy to read, and I can understand exactly what you’re trying to do if you were to hand this to another developer. :slight_smile:

1 Like

I appreciate this feedback so much!!

Definitely one of the more cleaner scripts I’ve made in my opinion :wink:

1 Like

Instead of creating the UI inside the script, you should have a premade UI made in studio that you can edit in script. It makes things easier to edit, visualize, and its just less lag.

More of a preference, but you don’t need to make a local for the tweenInfo, you can just put it inside the Tween like

local TweenToLeftDoor = TweenService:Create(camera, TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), {CFrame = LeftCamera.CFrame})

Here, since you aren’t using the tween after completion, you remove the local and just play it in 1 line. If you wanted to check when the tween was finished or cancel it you’d still want to make a local of it and play on the next line.

TweenService:Create(camera, tweenInfo, {CFrame = LeftCamera.CFrame}):Play()
1 Like

Extremely helpful, I will apply these to my games! Thank you for taking the time to critique it, I learned some new things as well. :slight_smile:

1 Like