How could i make this live camera system less laggy?

Decided i wanted to make a camera system just like snapchat in-game where there is a live updating camera showing everything infront of the character to prevent lag.

except it still lags (averaging 700 FPS before dropping to 32 when using this)

code:

--

local PlayerService = game:GetService("Players")

--

local LocalPlayer = PlayerService.LocalPlayer
local Character = LocalPlayer.Character

local Mouse = LocalPlayer:GetMouse()

--

local ViewportFrame = script.Parent.ViewportFrame
local Indication = false

--

local Toggle = function()
	while Indication do
		game["Run Service"].Heartbeat:Wait()
		
		if game.ReplicatedStorage:FindFirstChild("view") then
			game.ReplicatedStorage:FindFirstChild("view"):Destroy()
		end

		local Camera = workspace.CurrentCamera:Clone()
		Camera.Name = "view"
		Camera.Parent = game.ReplicatedStorage
		Camera.CameraType = Enum.CameraType.Scriptable
		
		ViewportFrame.CurrentCamera = Camera
		
		for index , v in ipairs(workspace:GetDescendants()) do
			if v:IsA("Part") or v:IsA("UnionOperation") and v.Parent ~= Character then
				local result = workspace:Raycast(Character["HumanoidRootPart"].Position, (Character["HumanoidRootPart"].CFrame.LookVector * 100))
				
				if result and result.Instance == v then
					v.CastShadow = false
					if ViewportFrame:FindFirstChild(v.Name) then
						if ViewportFrame:FindFirstChild(v.Name) == v then
						--	ViewportFrame:FindFirstChild(v.Name).Position = v.Position
						else
							v:Clone().Parent = ViewportFrame
						end
					else
						v:Clone().Parent = ViewportFrame
					end 
				else
					if ViewportFrame:FindFirstChild(v.Name) then
						if ViewportFrame:FindFirstChild(v.Name) == v then
							ViewportFrame:FindFirstChild(v.Name):Destroy()
						end
					end 
				end
			end
		end
	end
end

Mouse.Button1Down:Connect(function()
	local target = Mouse.Target
	
	if target then
		Indication = true
		Toggle()
	end
end)

Mouse.Button1Up:Connect(function()
	local target = Mouse.Target

	if target then
		Indication = false
	end
end)

Do you need to keep cloning the camera? You have this block of code inside the RunService.

local Camera = workspace.CurrentCamera:Clone()

Camera.Name = view
Camera.Parent = game.ReplicatedStorage
Camera.CameraType = Enum.CameraType.Scriptable

Is it creating infinite copies of the camera?

1 Like

Yes that’s the solution, he’s duplicating the camera each time the runservice heartbeat is ran.

yeah agreed. that’s your problem its creating an infinite number of cameras. is there anywhere else in the code where u can put the camera duplicates?

u see this will create a camera for every single frame:

		game["Run Service"].Heartbeat:Wait()
		
		if game.ReplicatedStorage:FindFirstChild("view") then
			game.ReplicatedStorage:FindFirstChild("view"):Destroy()
		end

		local Camera = workspace.CurrentCamera:Clone()
		Camera.Name = "view"
		Camera.Parent = game.ReplicatedStorage
		Camera.CameraType = Enum.CameraType.Scriptable

this is what id do, have a create camera function and have it cloned when they open the phone (like snapchat i think is whatchu wanted idk)

local function createCamera()
		local Camera = workspace.CurrentCamera:Clone()
		Camera.Name = "view"
		Camera.Parent = game.ReplicatedStorage
		Camera.CameraType = Enum.CameraType.Scriptable
end

while Indication and wait() do
		game["Run Service"].Heartbeat:Wait()
		
		if game.ReplicatedStorage:FindFirstChild("view") then
			game.ReplicatedStorage:FindFirstChild("view"):Destroy()
		end
1 Like