With help from a friend we have made a chromatic aberration script which fires from an remote function, However after a couple seconds of being fired the games framerate lowers exponentially to around 5-15, is it a way the code is written and is there any way to optimize it?
The script is fired in a server script which gets shortly destroyed after being fired
Boss is the enemy, script.Parent.parent is the arena and hrp is the player.
game.Lighting.glitcheff.OnClientInvoke = function(who,real)
spawn(function()
local RunService = game:GetService('RunService')
local UserInputService = game:GetService("UserInputService")
local instance,newRay = Instance.new,Ray.new
local v2,v3,cf,udim2 = Vector2.new,Vector3.new,CFrame.new,UDim2.new
local insert,random,abs = table.insert,math.random,math.abs
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local red = script.Parent.red:Clone()
red.Parent = script.Parent
local Offset = cf(0,0,0)
local Camera = game.Workspace.CurrentCamera:Clone()
red.CurrentCamera = Camera
CameraSubject2 = Character:WaitForChild("HumanoidRootPart")
Camera.CameraSubject = CameraSubject2
Camera.CameraType = Enum.CameraType.Follow
local ValidClasses = {
["MeshPart"] = true; ["Part"] = true; ["Accoutrement"] = true;
["Pants"] = true; ["Shirt"] = true;
["Humanoid"] = true;
}
local function RenderHumanoid(Model, Parent, MainModel)
local ModelParts = Model:GetDescendants()
for i=1, #ModelParts do
local Part = ModelParts[i]
if ValidClasses[Part.ClassName] then
local a = Part.Archivable
Part.Archivable = true
local RenderClone = Part:Clone()
Part.Archivable = a
if Part.ClassName == "MeshPart" or Part.ClassName == "Part" then
PartUpdater = RunService.Heartbeat:Connect(function()
if Part then
RenderClone.CFrame = Part.CFrame
else
RenderClone:Destroy()
PartUpdater:Disconnect()
end
end)
elseif Part:IsA("Accoutrement") then
PartUpdater = RunService.Heartbeat:Connect(function()
if Part then
if RenderClone.Handle then
RenderClone.Handle.CFrame = Part.Handle.CFrame
end
else
RenderClone:Destroy()
PartUpdater:Disconnect()
end
end)
elseif Part.ClassName == "Humanoid" then
RenderClone:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Running, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Landed, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Flying, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Seated, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.PlatformStanding, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
RenderClone:SetStateEnabled(Enum.HumanoidStateType.Physics, false)
end
RenderClone.Parent = Parent
end
end
end
local a = Random.new()
local function Render()
if real == true then
red:ClearAllChildren()
local Char = instance("Model")
Char.Name = ""
Char.Parent = red
RenderHumanoid(who,Char)
spawn(function()
while real == true do
RunService.Heartbeat:Wait()
local ran = math.random(1,3)
if ran == 2 then
red.Ambient = Color3.new(0, 1, 1)
red.LightColor = Color3.new(0, 1, 1)
red.ImageColor3 = Color3.new(0, 1, 1)
elseif ran == 3 then
red.Ambient = Color3.new(0, 1, 0)
red.LightColor = Color3.new(0, 1, 0)
red.ImageColor3 = Color3.new(0, 1, 0)
else
red.Ambient = Color3.new(1, 0, 0)
red.LightColor = Color3.new(1, 0, 0)
red.ImageColor3 = Color3.new(1, 0, 0)
end
Camera.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(a:NextNumber(-.3,.3),0,a:NextNumber(-.3,.3))
Camera.FieldOfView = 70
end
end)
end
end
Character.DescendantAdded:Connect(Render)
Character.DescendantRemoving:Connect(Render)
Render()
CameraUpdater = RunService.Heartbeat:Connect(function()
if Character.HumanoidRootPart then
Camera.CFrame = cf(Character.HumanoidRootPart.CFrame:toWorldSpace(Offset).p, Character.HumanoidRootPart.CFrame.p)
Camera.FieldOfView = 70
end
end)
end)
end
This is the code there are no errors whatsoever so optimization help would be appreciated.