Mass cloning for this one mesh causes lag

When I clone a large amount of just this type of mesh it causes lag.
It could be the amount of triangles but we already tried to reduce the amount, but didn’t fix much.
With the other meshes, they don’t lag at all?

2 Likes

Seems like a performance issues. It would be helpful if you could show the code.

1 Like

Here is the script

local function quadBezier(p1, p2, p3, t)
		return (1 - t)^2 * p1 + 2 * (1 - t) * t * p2 + t^2 * p3
	end
	
	local StandModel = char:WaitForChild("Stand")
	local CF = Offset or char.HumanoidRootPart.CFrame
	local Stretch = Stretch or 1
	for i = 1,3 do

		local lookTo = (CF * CFrame.new(0,0,-10)).Position
		local p1, p2
		local Arm
		local function PointGenerator(Side)
			local rand1
			if Side == "Right" then
				rand1 = math.random(5,30) / 10 * Stretch
			else
				rand1 = math.random(-30,-5) / 10 * Stretch
			end

			local rand2 = math.random(-50,50) / 100 * Stretch
			local rand3 = math.random()
			local p1 = (CF * CFrame.new(rand1,rand2,-2)).Position
			local p2 = (CF * CFrame.new(rand1 * 2,rand2 * 6,-4)).Position

			return p1,p2
		end
		if math.random(10) > 5 then
			local mainArm
			if StandModel:FindFirstChild("Stand Right Arm") then
				mainArm = StandModel:WaitForChild("Stand Right Arm")
			end
			if mainArm then
				Arm = mainArm:Clone()
			end
			p1,p2 = PointGenerator("Right")
		else
			local mainArm:BasePart
			if StandModel:FindFirstChild("Stand Left Arm") then
				mainArm = StandModel:WaitForChild("Stand Left Arm")
			end
			if mainArm then
				Arm = mainArm:Clone()
			end
			p1,p2 = PointGenerator("Left")
		end
		for i,v in pairs(Arm:GetDescendants()) do
			if v:GetAttribute("transparency") then
				v.Transparency = v:GetAttribute("transparency")
			end
		end
		
		Arm.Parent = game.ReplicatedStorage
		
		local TopAttachment = script:WaitForChild("BarrageTrail").A:Clone()
		TopAttachment.Parent = Arm
		local BottomAttachment = script:WaitForChild("BarrageTrail").B:Clone()
		BottomAttachment.Parent = Arm

		local Trail = script:WaitForChild("BarrageTrail"):WaitForChild("Trail"):Clone()
		Trail.Attachment0 = TopAttachment
		Trail.Attachment1 = BottomAttachment
		Trail.Enabled = true
		Trail.Parent = Arm
		
		local Trail1 = script:WaitForChild("BarrageTrail"):WaitForChild("Trail1"):Clone()
		Trail1.Attachment0 = TopAttachment
		Trail1.Attachment1 = BottomAttachment
		Trail1.Enabled = true
		Trail1.Parent = Arm

		for i,v in pairs(Arm:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				v:Destroy()
			end
		end
		
		Arm.Parent = char
		Arm.Transparency = Arm:GetAttribute("transparency") or 0
		local Count = 0
		local Connection
		local Duration = Duration or .5
		local rate = 1/Duration
		coroutine.wrap(function()
			wait(0.1)
			for i,v in pairs(Arm:GetDescendants()) do
				if v:IsA("BasePart") or v:IsA("MeshPart") then
					game.TweenService:Create(v,TweenInfo.new(0.2),{Transparency = 1}):Play()
				end
			end
			for i,v in pairs(Arm:GetDescendants()) do
				if v:IsA("Decal") then
 					game.TweenService:Create(v,TweenInfo.new(0.2),{Transparency = 1}):Play()
				end
			end
			game.TweenService:Create(Arm,TweenInfo.new(0.2),{Transparency = 1}):Play()
			Trail.Enabled = false
			Trail1.Enabled = false
			wait(0.25)
			Arm:Destroy()
		end)()
		Connection = game["Run Service"].RenderStepped:Connect(function(r)
			Count += r * rate
			if Count > 1 then
				Connection:Disconnect()
				Count = 1
			end
			if Count + r * rate <= 0.8 then
				local val = game.TweenService:GetValue(Count, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
				local nextVal = game.TweenService:GetValue(Count + r * rate, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
				local pos = quadBezier(p1, p2, lookTo, val)
				local nextPos = quadBezier(p1, p2, lookTo, nextVal)
				Arm.CFrame = CFrame.new(pos, nextPos) * CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))
			else
				local val = game.TweenService:GetValue(Count, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
				local pos = quadBezier(p1, p2, lookTo, val)
				Arm.CFrame = CFrame.new(pos, lookTo) * CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))
			end
		end)
	end

BTW this is all client and it is being fired from a loop in a server module

kinda need help on this asap, because its an important part of the game