I need help optimizing my after image effect
So i have this afterimage effect in my battlegrounds game but whenever i use it my frames drop instantly from 144 to ~90, I tried everything i could but the performance drop is still huge
Heres the shadow model
Heres the script:
local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Packages = ReplicatedStorage.Packages
local Assets = ReplicatedStorage.Assets
local Signal = require(Packages.Signal)
local Trove = require(Packages.Trove)
local EdgerunnerAssets = Assets:WaitForChild("Edgerunner")
local Shadow = EdgerunnerAssets.Shadow
local FX = workspace.Terrain:FindFirstChild("FX")
local EndSandevistanSignal = Signal.new()
local SandevistanGradients = {
Color3.fromRGB(83, 255, 103),
Color3.fromRGB(83, 255, 226),
Color3.fromRGB(0, 34, 255),
Color3.fromRGB(255, 42, 74),
}
local FullTransition = 6.0
return {
["SandevistanStart"] = function(Params: { Character: Model, UserId: number })
local Character = Params.Character
local UserId = Params.UserId
if not Character then
return
end
local Humanoid = Character:FindFirstChildOfClass("Humanoid") :: Humanoid
if Humanoid.Health <= 0 then
return
end
local Clones = {} :: { [Model]: number }
local GradientCount = #SandevistanGradients
local End = false
local EffectTrove = Trove.new()
local CharacterShadow = EffectTrove:Clone(Shadow) :: Model
CharacterShadow.Parent = FX
local ShadowHumanoid = CharacterShadow:FindFirstChild("Humanoid") :: Humanoid
local PlayerDescription = Humanoid:GetAppliedDescription()
ShadowHumanoid:ApplyDescription(PlayerDescription)
local ColorCorrection = Instance.new("ColorCorrectionEffect")
local ScreenEffect = TweenService:Create(ColorCorrection, TweenInfo.new(1), {
TintColor = Color3.fromRGB(121, 255, 119),
})
ColorCorrection.Parent = Lighting
EffectTrove:Add(ColorCorrection)
ScreenEffect:Play()
local EndConnection
do
EndConnection = EndSandevistanSignal:Connect(function(ID: number)
if ID == UserId then
EndConnection:Disconnect()
End = true
end
end)
end
local DeathConnection = Humanoid.Died:Once(function()
EndConnection:Disconnect()
End = true
end)
local DeltaConnection
do
local ElapsedTime = 0
local Progress = 0
DeltaConnection = RunService.Heartbeat:Connect(function(DeltaTime: number)
if End == true then
DeltaConnection:Disconnect()
EffectTrove:Destroy()
if DeathConnection.Connected then
DeathConnection:Disconnect()
end
table.clear(Clones)
return
end
ElapsedTime += DeltaTime
Progress += DeltaTime
for Clone, Data in pairs(Clones) do
local TimePassed = ElapsedTime - Data.Created
if TimePassed >= 1 then
local Alpha = (TimePassed - 1) / 0.5
Alpha = math.clamp(Alpha, 0, 1)
for _, BodyPart: BasePart in ipairs(Data.BodyParts) do
BodyPart.Transparency = Alpha
end
Data.Highlight.FillTransparency = 0.4 + (0.6 * Alpha)
if Alpha >= 1 then
Clone:Destroy()
Clones[Clone] = nil
end
end
end
if Progress > 0.15 then
Progress = 0
local Position = ((ElapsedTime % FullTransition) / FullTransition) * GradientCount
local Index = math.floor(Position) + 1
local Alpha = Position - math.floor(Position)
local NextIndex = (Index % GradientCount) + 1
local StartColor = SandevistanGradients[Index]
local NextColor = SandevistanGradients[NextIndex]
local Clone = EffectTrove:Clone(CharacterShadow) :: Model
local CloneColor = Clone:FindFirstChild("Color") :: Highlight
local BodyParts = {}
for _, BodyPart: BasePart in ipairs(Clone:GetChildren()) do
local CharacterBodyPart = Character:FindFirstChild(BodyPart.Name) :: BasePart
if BodyPart:IsA("BasePart") and CharacterBodyPart then
BodyPart.CFrame = CharacterBodyPart.CFrame
table.insert(BodyParts, BodyPart)
elseif BodyPart:IsA("Accessory") then
local Handle = BodyPart:FindFirstChild("Handle") :: BasePart
table.insert(BodyParts, Handle)
end
end
CloneColor.FillColor = StartColor:Lerp(NextColor, Alpha)
Clone.Parent = FX
Clones[Clone] = {
Created = ElapsedTime,
BodyParts = BodyParts,
Highlight = CloneColor,
}
end
end)
end
end,
["SandevistanEnd"] = function(UserId: number)
EndSandevistanSignal:Fire(UserId)
end,
}
Thanks!
