Detect anything near a part

i want to detect anything that is within a 5 blocks radius of a part . i want to find a solution that doesnt use another part to GetTouchingParts()

3 Likes

I think it involves using the magnitude property.
And also with magnitude, it detects the distance something is away from the part. But you have to calculate it individually for EVERY SINGLE PART, so I don’t reccomend it too much.
You would be better with going with the part solution with the post.

2 Likes

It wouldn’t be a great idea to check magnitude between parts because you would need to loop through every single part in the workspace. It would be better to use Region3 where you can use a function similar to GetTouchingParts, but more reliable, FindPartsInRegion3.

If you went with this method, it would be done something like this.

function GetPartsInArea(Position, MaxDistance)
Objects = {}
for _, object in pairs(workspace:GetChildren()) do --if you want to check the workspace
if object: IsA("BasePart") then
if (Position - object.Position).Magnitude < MaxDistance then
table.insert(Objects, object)
end
end
end
return Objects
end

To use the function:

PartsInRadius = GetPartsInArea(HumanoidRootPart.Position, 5) -- this would get all parts within a radius of 5 from the HumanoidRootPart in this case.

However, unless you have very small amount of workspace content, this is extremely inefficient, as you have to loop through everything. A better solution is to use Region3s as @MayorGnarwhal said.

1 Like

can you elaborate on this? i dont understand how you would get objects in a 5 block radius

I updated my post with a application of the function. I still don’t recommend this though, as it’s terrible performance wise.

2 Likes