How can I reduce lag from this?

So I made this somewhat cool rainbow aura thing in my game


However, I wanted to know what the best way to go about coding this is? I originally used a while true do loop which just makes the parts, tweens it, and then deletes it although after a while it begins to lag I guess it’s because of the number of parts being created at once so I switched to using render step which didn’t seem to help much and then finally some random method I found which seems to work ut a bit confusing to use.

Here’s the code used for the while loop version.

	coroutine.wrap(function()
		while true do
			local newPart = workspace.MeshPart:Clone() 
			newPart.Anchored = true
			newPart.CanCollide = false
			newPart.Transparency = 0.5
			newPart.Position = HRP.Position + Vector3.new(math.random(-3,3), 0, math.random(-3,3)) 
			if color == "rainbow" then
				RainbowEffect.Rainbow(newPart,5)
			else
				newPart.Color = color
			end
			newPart.Material = Enum.Material.Neon
			newPart.Parent = workspace.Game.FX

			local info = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,false,0)
			local goals = {Position=newPart.Position + Vector3.new(0,math.random(1,3),0),Transparency=1}
			local lastTween = TS:Create(newPart,info,goals)
			lastTween:Play()

			game.Debris:AddItem(newPart,0.9)
		end
	end)()
2 Likes

First of all, you should handle effects like these on the client (if you already are then good job). Handling it on the server makes the server need to create and move each part then replicate them to the client. (aka causing massive network lag). Instead if you send a message to the client to replicate an effect then the client can handle creating and tweening the parts without any network usage at all!

Second, to not lag by making new meshparts you can use partcache. PartCache, for all your quick part-creation needs

Third, (this is probably the answer to your question) I was trying to make a gun script so for my idea of tracers i used meshparts, this got really laggy, and then i set the meshparts RenderFidelty to performance and it removed most of the lag.

yeah I already do all my effects on the client.

The partcache module seems really cool although I can’t seem to figure out how to use it haha do you happen to know how I could use it with the code I provided?

Nice, handling effects on the client is a really good practice

also sure! PartCache is super simple to use

local PartCache = require("partcache")--replace with partcache modulescript
--I usually make a global table of effects in partcache
--just loop through all the effects and create a cache

--then call partcache.new()
local Cache = PartCache.new(template, numtocreate, parent)
--template is the effect you want to create
--numtocreate is the amount you should create (this module will create more if it runs out)
--parent is the parent for all the parts, make it a folder in workspace you can easily add to a raycast ignorelist

--now to call your part do
local Part = Cache:GetPart()
--to return it do
Cache:ReturnPart(Part)

alright so this should be correct then?

	--cool bubble thing
	local orb = Utils:MakePart(nil,nil,0,"Ball",Color3.fromRGB(255,255,255),"Neon",nil)
	local Cache = PartCache.new(orb,20,workspace.Game.FX)

	task.spawn(function()
		while true do
			for i = 1,5 do
				local sphere = HRP.Position
				local radius = 6
				local random_x = math.random(-1,1)
				local random_y = math.random(-1,1)
				local random_z = math.random(-1,1)
				local random_vector = Vector3.new(random_x, random_y, random_z)
				local random_direction = random_vector.Unit
				local random_direction_with_radius = random_direction * radius
				local random_point_on_surface = sphere + random_direction_with_radius

				local clone = Cache:GetPart()

				clone.Size = Vector3.new(0.2,0.2,0.2)
				RainbowEffect.Rainbow(clone,3)
				clone.CFrame = CFrame.new(random_point_on_surface)

				local tween = Utils:Tween(clone,{CFrame=HRP.CFrame},0.5,"Linear","In")
				tween:Play()

				tween.Completed:Connect(function()
					Cache:ReturnPart(clone)
				end)
			end

			task.wait(0.2)
		end
	end)

Edit: By the way my understanding is the ReturnPart method just teleports the part far away so that it’s no longer going to be rendered however this effect runs until the player activates an ability so wouldn’t this just keep creating a bunch of parts?

create the cache at the top of the script instead of inside the repeat function and that should be it

Edit: your returnpart understanding is right, it also returns it into the table so it can be called by :GetPart() aswell

Try to trigger the function/event only once. Well, after that make sure it doesn’t have a defined time smaller or larger than the interpolation time (If you want to use it, just use task.wait).

Leave the “Connect” variable out of any event, as it is a variable that can be modified only once.

type Dictionary = Instance | {any} | {[any]: any}

local RunService: RunService = game:GetService("RunService")

local TimeInterpolate: number = 1
local Connect: boolean = false

if not Connect then
    Connect = true;
    RunService:BindToRenderStep("Effect", Enum.RenderPriority.Character.Value, function()
        coroutine.wrap(function()
            local NewPart: Part = workspace.MeshPart:Clone() 
            NewPart.Anchored = true
            NewPart.CanCollide = false
            NewPart.Transparency = 0.5
            NewPart.Position = HRP.Position + Vector3.new(math.random(-3,3), 0, math.random(-3,3)) 
            
            if color == "rainbow" then
                RainbowEffect.Rainbow(NewPart,5)
            else
                NewPart.Color = color
            end
        
            NewPart.Material = Enum.Material.Neon
            NewPart.Parent = workspace.Game.FX
        
            local Info = TweenInfo.new(TimeInterpolate, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
            local Goals: Dictionary = {Position = NewPart.Position + Vector3.new(0,math.random(1,3), 0), Transparency = 1}
            local LastTween = TS:Create(NewPart, Info, Goals)
        
            LastTween.Completed:Connect(function()
                NewPart:Destroy()
            end)
        
            LastTween:Play()
        end)()
    end)
end