Why does this script cause so much network usage?

i’m working on a bullet trail script and i finished it but i found out that it causes a lot of network usage. (pic below)
image

here is the script: (client)

while true do 
  wait(0.1) 
  local trail = Instance.new("Part") -- part with a trail inside it (removing it doesnt change anything)
  trail.Parent = workspace.Debris
  trail.Position = Vector3.new(0,0,0) -- some position (doesnt matter)
  local BodyVelocity = Instance.new("BodyVelocity")
  BodyVelocity.P = Vector3.new(10000,10000,10000)
  BodyVelocity.Velocity = workspace.CurrentCamera.CFrame.LookVector * 250 -- where to direct the part
  BodyVelocity.Parent = trail 
end

note: this isnt the exact same script, i replaced some stuff so you can test it out

how can i fix this?

1 Like

Never ever ever parent a part before you set all of its properties! This is a major bottleneck for any roblox instancing code and should be avoided at all costs. Put the .Parent line after setting the position and see if it makes a large difference. Furthermore, do you really need to instance a new part that frequently? Maybe for a trail you could use beams or particle emitters.

Hope this helps.

Edit: Seeing that you only set the position to the origin, it’s probably just the fact that you instance a new part and constraint so frequently. This will also cause major lag when you add more players.

4 Likes

putting the parent line after setting the position and everything or removing the Instance.new doesnt seem to change anything.

here is what im at rn:

        pcall(function()
		local trail = game.ReplicatedFirst.Models.trail:Clone() -- part with trail inside it
		trail.Position = workspace.CurrentCamera.ViewModel["Right Arm"][weapon].Main.Position --
		trail.Trail.Lifetime = 0.13 * 1/(deltaTime*60) * 2

		local BodyVelocity = Instance.new("BodyVelocity",trail)
		BodyVelocity.P = Vector3.new(10000,10000,10000)
		BodyVelocity.Velocity = cframe.LookVector * 250
		trail.Parent = workspace.Debris
                game.Debris:AddItem(trail,4)
	end)

also, the high network usage stays even after all the parts have been deleted from workspace and the script has been disabled.

1 Like

In my opinion Setting the Parent of trail after the line BodyVelocity.Parent = trail is better, I heard is good for performance.

2 Likes

I recommend looking into particle/beams emitters instead of creating multiple parts each second.

2 Likes

That is the best solution for OP. Trails are likely the most network-efficient way of creating bullet trails, and are easily customization. No matter how you handle it, creating a part every frame (or every 0.1 seconds, in your case) is going to have a toll on performance (not to mention also creating a BodyMover and running those physics every frame too, yikes).

They’re not the most difficult to implement (see here) and should suit most people’s needs.

1 Like

i found a solution to this kinda

i removed the BodyVelocity and replaced it with:

trail.Velocity = cframe.LookVector * 250

this lowered the network usage by like 90% so its pretty much fixed

also: this whole time i was running the code on client, so the high network usage when using a BodyVelocity might’ve been a roblox bug idk

1 Like