Need a way to get how many particles in a part

So I am trying to get the amount of particles in a part.

GetTouchingParts will not work because the particles are not objects but tables that has a position and a velocity.

I tried looping through all the particles and checking if they are inside the cell or not, but that was super laggy.

Here is the functions I am using.

function module:ParticleIn(Cell:BasePart, Particle)
	local Position:Vector3 = Particle.Position
	
	local CellSize = Cell.Size
	local Center = Cell.Position
	
	local MinX = Center.X - CellSize.X / 2
	local MaxX = Center.X + CellSize.X / 2
	local MinY = Center.Y - CellSize.Y / 2
	local MaxY = Center.Y + CellSize.Y / 2
	local MinZ = Center.Z - CellSize.Z / 2
	local MaxZ = Center.Z + CellSize.Z / 2
	
	local Inside = Position.X >= MinX and Position.X <= MaxX and
				   Position.Y >= MinY and Position.Y <= MaxY

	return Inside
end

function module:CellParticles(Cell:BasePart, Particles)
	local ParticlesCount = 0
	
	for I, Particle in pairs(Particles) do
		if module:ParticleIn(Cell, Particle) then
			ParticlesCount += 1
		end
	end
	
	return ParticlesCount
end

I need to run the CellParticles function about 1000 times every frame.

I tried to make the ParticleIn function just return false without calculating, And the lag was almost gone. So I think the ParticleIn function is causing the problem.

10 Likes

GetTouchingParts will not work because the particles are not objects but tables that has a position and a velocity.

5 Likes

are you running this on the server?

3 Likes

Theres no real way to make that function less laggy since its basically as efficient as a collision check could be, i just think running a collision calculation 1000 times a frame may not be the greatest idea, what are you using it for?

4 Likes

How does roblox calculate the collision without lag?

3 Likes

And the ParticleIn function is the function that seems to lag not the CellParticles function.

2 Likes

Their collision engine runs on a different language hwich is faster than lua

2 Likes

Does it lag when running less times per frame?

2 Likes

I could make it run slower and save the result in a table (I think this is called baking)

But I think there is a way to do it without a lot of lag, Because the ParticleIn is the only function that is lagging, everything else seems to be fine.

3 Likes

Isn’t luau also fast? (not lua)

3 Likes

Luau is pretty much just lua with extra functions i think

2 Likes

What are you using the particle thing for there could be an easier wya

2 Likes

I am trying to make a simple fluid simulation btw, And that has already been done before with no lag.

2 Likes

Ohh right, im not sure how much i can help you there, sorry

1 Like

Yes, I am running this on the server not the client.

I think it would be more laggy if I ran it on the client (not too sure tho.)

2 Likes

try to run it on the client, maybe it will be faster

2 Likes

I tried it on the client it was still slow (I think it was even slower).

2 Likes

I don’t think there is a way to make this faster

lua is really slow

2 Likes

Yea but roblox uses luau, Which I heard is a lot faster than lua.

2 Likes

Maybe there are easier ways to make a fluid simulation, try searching

2 Likes