How to move lots of projectiles with no lag?

Hi Everybody! This is my first ever Post so sorry if this is weirdly organized.
Just as background info, I’m not a good scripter at all. I just like to script as a hobby for fun when I’m bored and have no homework or stuff

Recently I started making a recreation of Diep.io because as a kid i always played it and since I’m bored, I thought I would try making it on studio for fun.

Before i say anything else I did find a post of another person recreating diep.io : I recreated Diep.io on Roblox and i just want to say that im not trying to copy them and I didn’t even knew this post existed til now, im just making my recreation for fun.

Now the current problem i have is that when moving loads of projectiles, it starts to lag and slows down the game. The current movement system for my projectiles is this :

local BulletSpeed = 1--script.Parent.BulletSpeed.Value
local SpawnedExtraMovement = BulletSpeed + 0.2 -- when a projectile first spawns it has a bonus movement speed which will slow down as the bullet travels
local SpawnedYValue = script.Parent.Position.Y

while true do
	script.Parent.BodyPosition.Position = Vector3.new(script.Parent.Position.X,SpawnedYValue,script.Parent.Position.Z) + (script.Parent.CFrame.LookVector*10)*SpawnedExtraMovement
	if SpawnedExtraMovement > BulletSpeed-0.4 then
		SpawnedExtraMovement = SpawnedExtraMovement-0.02
	end
	
	wait(0.03)
end

Heres some examples of how the game/projectiles work

When its only a small amount of projectiles, there really isint any lag.

Now heres a example of 1 person shooting alot of projectiles ( but still not too much of lag )

(Yes I know the projectiles are jittery but when it comes to lag its not much of a issue)

Now this is where the game starts the lag, im going to have fake players called “AutoShootTest” that simulate what having multiple players shooting at the same time looks like

As you can see the game starts to slow down and lag
The lag can also be seen from the Dominator AI (The Giant Yellow Circle AI thingy) where the cannon of the AI starts to spin very slowly due to lag

Now i have tryed 2 other methods for moving my projectiles :

  1. TweenService – i actually prefer using tween to move objects however for this situation it gives the same laggy result when there loads of projectiles at the same time.

  2. FastCast – Ive tryed looking into FastCast however like I said I’m not a good scripter so this is complicated for me and plus after reading a bit, it just not meant for this type of game as im not trying to make cosmetic bullets

So if there’s any other ways of moving loads of projectiles for this type of game, please let me know! Otherwise I’m basically going to quit working on this project cause i don’t want a laggy game.

Thanks for reading.

2 Likes

I do not believe moving parts is the laggy bit of the script.

It’s creating these parts. Creating new instances is expensive as it has to replicate to all clients that part has been created and incurring significant remote lag. This is proven with Alvinbloxes experience.

So instead of creating new instances, try creating a set amount then reuse them. This should remove the part creation lag and only focus on the part moving lag with position or CFrames which is minimal.

Personally I have noticed considerable improvement as well when using it with fastcast on the client, so it should be the same with the server.

Edit: oh yeah don’t use wait() especially for this scenario

Use task.wait or else it’ll become slow and throttle especially when spammed.

Unlike the global wait, this function does not throttle and guarantees the resumption of the thread on the first Heartbeat that occurs when it is due. This function also only returns the elapsed time and nothing else.

So the problem might be more than one, one for remote/network lag and another for script lag (due to wait() being used)

2 Likes

Actually that’s how I’m using projectiles, I spawn about 100 bullets for each tanks then I just use them and re-use them. I’m basically using partcache but my version is simpler.
If I were to turn off the movement script and then have the fake players shoot again, it would result in no lag.

Here’s the same TestLagWithFakePlayers experiment but this time I’ve disabled the movement script and the lag is mostly gone now

also I’ll try using task.wait

Seems good, however how are you putting them away? Are you setting the parent to nil? If so then it would not be as efficient as PartCache. It would be similar to this other Part pooling module which had a similar issue.

Also I recommend using delta time. Like

local dt = task.wait()
local maintainYPosition = script.Parent.Position*Vector3.new(1,0,1)+Vector3.new(0,SpawnedYValue,0)
script.Parent.BodyPosition.Position = maintainYPosition + (script.Parent.CFrame.LookVector*10)*SpawnedExtraMovement*dt 

This will make the movement take into account of any delays and general make it more smoother. In this case it will guarantee the movement will go 10 studs per second without the extra movement speed.

I have two folders inside of the cannon called “SavedBullets” and “BulletsInUse”. I initially create about 100 projectiles parented to SavedBullets with their CFrame set to a high height,

When I need a bullet I grab one from SavedBullets, Parent it to BulletsInUse and CFrame movement etc, after it’s done being used I set the CFrame of the bullet back to really far away and parent it back to SavedBullets
(I’ve also tryed only cframing bullets without changing the bullets parent but gives the same laggy result)

Also I’m now using delta time (but of course I still have the lag issue)

Oh well I made it for fun anyways, thanks for trying to help though!

actually I’m gonna try 1 last idea which is having 1 script move projectiles instead of a separate script for each projectile
(aka 200 bullets would be 200 while true do move scripts)
(But now Ima try 200 bullets being controlled by 1 script)

Nope, it gave the same result, oh well I guess I’ll just stop working on this project

Nvm apparently it was laggy because I was playing on a 5 year old iPad. I recently bought a brand new iPad Pro and when I revisited the game and did some bug fixes the game doesn’t really lag anymore.