Detecting When Im Close To A Lot Of Parts

  1. What do you want to achieve? Keep it simple and clear!
    I want to detect the closest parts from my character without using for loops
  2. What is the issue? Include screenshots / videos if possible!
    When I use for loops everything gets laggy
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Dividing the parts by regions, the problem is I don’t know how to detect when the character has left a region.

For example, I want to detect only parts that are in a radius of 500 studs around my character without for loops.

1 Like

Your best bet here will be Magnitude, in which you will need a loop.

There are some ways you can minimize the lag, such as using CollectionService or a folder in workspace to only loop through the parts you want to detect.

local player = game.Players.LocalPlayer
local character = player.Character
local radius = 500
local origin = character.HumanoidRootPart.Position

local parts = {}
for i, part in pairs(workspace:GetDescendants()) do
	if (part.Position - origin) <= radius then
		table.insert(parts, part)
	end
end
2 Likes

I don’t want to use for loops.

I have about 10 million positions so…

3 Likes

Oh jeez could you explain exactly what you’re trying to do/what’s the goal? Using the client to find the parts could be a better option if it’s possible.

3 Likes

Your problem seems to be a problem in your game’s design, a simple loop like this should not be lagging your game. This means you probably have too many loops and too many unoptimized scripts, and a loop is the only way to do what you are trying to achieve.
Mark as solution if this helps!

1 Like

I actually use the client.
See:

I’m working on procedural planet generation, terrain generation was very nice until I tried it on multiplayer.
Internet errors, kicking, 85 seconds to load in, etc.

I’m making a terrain rendering system that divides each 65 million studs surface into about 8 regions, cause if I loop through all of the 10 million positions even without filling the terrain it lags as hell.

Thats why I divided planets into 8 regions, now the problem was that I wasn’t able to know when a character left the region to start loading another region.

I tried using parts the problem is that the spheres warped towards the poles the closest they get so it was terrible to do that.

I tried remastering the same system, the problem was that it detected me colliding with it about 4 times per pass so if you could help me fixing that out this would be solved.

So my second option was to trying to detect the closest positions without a for loop

a simple loop is not that one that loops through 10 million positions every time a frame renders

I mean, there’s your problem. I don’t see how anyone remotely sane could think roblox could handle that. Perhaps you need to scale down your project a bit.

I can’t thats why I divided planets into 8 regions, it fixed lag, now my problem is that I don’t know how to detect when the character has left a certain region.

If you could help me fixing a simple touching issue this would be solved

I’d suggest taking a look at chunk loading.

This game is uncopylocked and does a pretty good job of chunk loading.

The problem is this issue isn’t “simple”
Technically you don’t need to loop, as you could check whenever the player’s movedirection changed for their position. But it would basically be the same as a loop while the player is actively walking and would still probably lag out your game.

I was referring to a .Touched:Connect(function() issue.

Simple as that, it was the system I tried using to detect when the players enters or leaves a region

it loads terrain?
And how would I divide a sphere into square chunks, the generator code basically stops me from doing that

or maybe not, I thought about something

Bad idea, touched events are unreliable and will likely lead to the area the player is in not being detected correctly.

I had it semi-working the problem was that it detected my about 4 times even after setting every character’s parts cantouch to false.

I just need to fix this

nvm it won’t work, it will exhaust the code

you could try WorldRoot:GetPartBoundsInRadius

2 Likes

does it works with cframe values?

Why not have the bigger regions loaded in, but subdivide them into smaller areas. You could detect which areas are closer, not the acutal parts inside them. If the areas are inside your value of number of studs away in Magnitude (I hope you aren’t using each Parts actual Postition for the calculations!) then you can have your script recognize them.

1 Like