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
What is the issue? Include screenshots / videos if possible!
When I use for loops everything gets laggy
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.
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
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.
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!
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
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
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.
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.