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)