Best way to actively get parts within an area

Some I have been making a car spawn GUI and I need to check if the spot is occupied with another car before the car spawns so it does not bug out but I cant seem to find the best way to get the parts within in an area that does not hinder performance for example using region3 but you would have to update that pretty often with a loop to see if the parts in the area have changed and touched events are very performance heavy. Im kind of stuck any thoughts would be appreciated

1 Like

You could add a invisible, anchored and non collideable part that covers the whole spot you want to check if its occupied. Then you give something to the car or if every car has the same name then you can just check if its being touched by something with the name of the car or touched by something that has the “special” thing that the car has.

So you do a TouchEnded:Wait() for the cooldown and then let the car spawn again

As a example:

local carSpawnAble = true
local spawnHitbox = the part you place at the spawn with the size of the spot
spawnHitbox.Touched:Connect(function(hit)
       if carSpawnAble == true then
          if hit.Name == "Car" then -- You specify a special thing the car has
             --Dont let the car be spawned by using something (I will use carSpawnAble)
             carSpawnAble = false
             spawnHitbox.TouchEnded:Wait()
             carSpawnAble = true
          end
       end
end)
1 Like

I would recommend using the Region3 library.

This could fire even if the car is still in the spawn area.
Touch fires a ton of times if you don’t add a debounce and that means TouchEnded will too.

2 Likes

This was my last solution it seemed buggy at times and seemed unreliable

I was thinking region3 but it needs to be updated often what is the most optimized loop (There are alot of car spawn areas)

1 Like

Loop through the spawns every so often and make a region3 check the parts inside and go from there. At least that is the best way I can think of doing it.

1 Like

I also needed to process parts within an area. What i do is create a region 3 during run time, and set the position i created it at. After i travel away from the distance set, i reset my region 3. This allows me to basically reduce the amount of constructions i do. I also use whitelisting so it only returns parts i need to do the processing for.

2 Likes

Thank you This was helpful 30 chars

1 Like