GetTouchingParts() is laggy

I would like to make a script which detect how many parts named “Sphere” a specific part touch.

GetTouchingParts() make lag because it detect too many part (sometime 1000+ part) and after, It have to check how many parts have this name. So, I would like to know how to make a GetTouchingParts() which only detect parts which have a specific name.

Can you help me please? My game will never be good if there are still these lags. Each time I have to detect parts, it make freeze the game during 0,25 seconds.

Are you sure :GetTouchingParts() is the problem? Its just an array of data,

I think having 1000 unanchored parts may be the problem.

You could maybe do

local function GetCertainTouchingParts(part, name)
  local name = string.lower(name)
  local t = {}
  for _, v in pairs(part:GetTouchingParts()) do
    if v.Name:find(name) then
      table.insert(t, v)
    end
  end
  return t
end

Parts are anchored.
Event with only 100 parts, the game freeze during a while.

You should refactor your code using GetPartBoundsInBox in combination with OverlapParams to massively speed up this process.

1 Like

Yeah, just whitelist a folder containing the parts you want to query.

I’ve personally never observed frame lag from “:GetTouchingParts()”, are you perhaps calling it too frequently, i.e; every frame?

I call it only every 4-8 seconds but It really make the screen freeze.

Also, when the game freeze, this is not because of the :GetTouchingParts() function. This is because the game have to check if the parent of each parts contain an humanoid. I wanted :GetTouchingParts() list have less parts to avoid lag.

Now, the game have to check if the part name is “HumanoidRootPart” instead checking if the parent of this part contain an humanoid.