How could I make this beam more optimized?

  1. What do you want to achieve?
    I want to make more optimized beam.

  2. What is the issue?
    I have made a script that creates this beam (Using Run service, creating parts every frame)

It works well but when it is too long or there are multiple beams at the same time it starts to lag.


  1. What solutions have you tried so far?

I have tried recreating this using the actual beams but it lagged even more.

Sorry for the bad English.

2 Likes

First of all, the parts should be castshadow, cancollide, cantouch, canquery false.

Maybe not clone the parts every frame? Seems like a bit overkill. Maybe every 2 or 3 frames.

Can we see your code?

1 Like

so i’m gonna assume ur using parts to create this beam effect. i’m pretty sure u could use beam instances that face the camera instead. (u could move the attachments that hold the beams every frame instead of creating parts)

as a bonus, u could make the effect client sided since it’s better for vfx smoothness and performance

1 Like

I tried using beams and moving attacments but it lagged even more somehow.

Yes but it is pretty messy as I was experimenting.
Also the parts that are being cloned have castshadow, cancollide, cantouch, canquery false.

local runs = game:GetService("RunService")
local target = script.Parent.target
local origin = script.Parent.origin

local raypart1 = game.ReplicatedStorage.Protongun1particles.set1.ray2.Value
local raypart2 = game.ReplicatedStorage.Protongun1particles.set1.ray1.Value
local raypart3 = game.ReplicatedStorage.Protongun1particles.set1.ray3.Value


local pdistance = 1.2 -- distance between points
local offset = 0
local currentparts = {}
local raysizediff = {0.25,0.25} --size offset

local seedsoffset = Vector3.new(math.random(1,2000),math.random(1,2000),math.random(1,2000))
local seedspeed =0.02
local seedrange = 0.1
local seedmulti = 4


local pdistance2 = 1.2
local offset2 = 0
local currentparts2 = {}
local raysizediff2 = {0.1,0.25}
local seedsoffset2 = Vector3.new(math.random(1,2000),math.random(1,2000),math.random(1,2000))
local seedspeed2 =0.15
local seedrange2 = 0.2
local seedmulti2 = 2

local pdistance3 = 1.2
local offset3 = 1
local currentparts3 = {}
local raysizediff3 = {0.1,0.25}
local seedsoffset3 = Vector3.new(math.random(1,2000),math.random(1,2000),math.random(1,2000))
local seedspeed3 =0.09
local seedrange3 = 0.2
local seedmulti3= 1

runs.Heartbeat:Connect(function()
	seedsoffset-=Vector3.new(seedspeed,seedspeed,seedspeed)
	seedsoffset2-=Vector3.new(seedspeed2,seedspeed2,seedspeed2)
	seedsoffset3-=Vector3.new(seedspeed3,seedspeed3,seedspeed3)
	for i,v in pairs(currentparts) do
		v:Destroy()
		v=nil
	end
	currentparts={}
	local pointsc = math.ceil((target.Position-origin.Position).Magnitude/pdistance-0.5)
	local points = {}
	local extrapoints1 ={}
	local extrapoints2 ={}
	table.insert(points,origin.CFrame)
	table.insert(extrapoints1,origin.CFrame)
	table.insert(extrapoints2,origin.CFrame)
	for count = 1,pointsc do
		
		
		table.insert(points,CFrame.new(origin.Position,target.Position)*CFrame.new(0,0,pdistance*-count)*CFrame.new(math.random(-offset*100,offset*100)/100+math.noise(seedsoffset.X+count*seedrange,1,1)*seedmulti,math.random(-offset*100,offset*100)/100+math.noise(seedsoffset.Y+count*seedrange,1,1)*seedmulti,math.random(-offset*100,offset*100)/100+math.noise(seedsoffset.Z+count*seedrange,1,1)*seedmulti))
		table.insert(extrapoints1,points[count]*CFrame.new(math.random(-offset2*100,offset2*100)/100+math.noise(seedsoffset2.X+count*seedrange2,1,1)*seedmulti2,math.random(-offset2*100,offset2*100)/100+math.noise(seedsoffset2.Y+count*seedrange2,1,1)*seedmulti2,math.random(-offset2*100,offset2*100)/100+math.noise(seedsoffset2.Z+count*seedrange2,1,1)*seedmulti2))
		table.insert(extrapoints2,points[count]*CFrame.new(math.random(-offset3*100,offset3*100)/100+math.noise(seedsoffset3.X+count*seedrange3,1,1)*seedmulti3,math.random(-offset3*100,offset3*100)/100+math.noise(seedsoffset3.Y+count*seedrange3,1,1)*seedmulti3,math.random(-offset3*100,offset3*100)/100+math.noise(seedsoffset3.Z+count*seedrange3,1,1)*seedmulti3))
	end
	table.insert(extrapoints1,target.CFrame)
	table.insert(points,target.CFrame)
	table.insert(extrapoints2,target.CFrame)
	for i,point in pairs(points) do
		if i~= #points then
		local npart = raypart1:Clone()--new part for the beam
		table.insert(currentparts,npart)
		npart.Parent=workspace
			npart.CFrame=CFrame.new(points[i].Position+(points[i+1].Position-points[i].Position)/2,points[i].Position)
			npart.Size=Vector3.new(math.random(raysizediff[1]*100,raysizediff[2]*100)/100,math.random(raysizediff[1]*100,raysizediff[2]*100)/100,(points[i+1].Position-points[i].Position).Magnitude)
			npart.Destroying:Connect(function()
				npart=nil
			end)
		end
		if i~= #extrapoints1 then
			if i~= #points then
			local npart = raypart2:Clone()
			table.insert(currentparts,npart)
			npart.Parent=workspace
			npart.CFrame=CFrame.new(extrapoints1[i].Position+(extrapoints1[i+1].Position-extrapoints1[i].Position)/2,extrapoints1[i].Position)
			npart.Size=Vector3.new(math.random(raysizediff2[1]*100,raysizediff2[2]*100)/100,math.random(raysizediff2[1]*100,raysizediff2[2]*100)/100,(extrapoints1[i+1].Position-extrapoints1[i].Position).Magnitude)
			npart.Destroying:Connect(function()
				npart=nil
			end)
			end
		end
		if i~= #extrapoints2 then
			if i~= #points then
				local npart = raypart3:Clone()
				table.insert(currentparts,npart)
				npart.Parent=workspace
				npart.CFrame=CFrame.new(extrapoints2[i].Position+(extrapoints2[i+1].Position-extrapoints2[i].Position)/2,extrapoints2[i].Position)
				npart.Size=Vector3.new(math.random(raysizediff3[1]*100,raysizediff3[2]*100)/100,math.random(raysizediff3[1]*100,raysizediff3[2]*100)/100,(extrapoints2[i+1].Position-extrapoints2[i].Position).Magnitude)
				npart.Destroying:Connect(function()
					npart=nil
				end)
			end
		end
	end
end)

there is a LOT that could be optimized; main thing causing lag is likely the creation and deletion of instances.
reuse parts, whether it just be singular initialized ones or through something like Object Pooling

2 Likes

Can you give more detailed explanation how could I make it?