How to get all parts near a player

Is there any way I can get all parts within a specific distance of a player? For example i’d want to print every part that is within 10 studs of a player. I’ve used magnitude before but doing “workspace:GetChildren()” or “workspace:GetDescendants()” seems like it would cause performance issues. I’d store the parts in a folder but i’d rather not, however if using a folder is the only way to achieve this i’ll do it. So is there any way I could get all parts near a player without cycling through all parts in the workspace or using a folder?

2 Likes

Just do a massive amount of raycasting around the player in attempt to find as many parts in proximity, as if you were to program a high-explosive grenade. That should do the trick.

Two things to note:

  1. Rate of read
  2. Count of rays(don’t make millions unnecessarily)
1 Like

You should create a region part that is welded to the player’s character and use Region3 to check what parts are inside the region.

3 Likes

Im extremely new to raycasting so this stuff is pretty confusing, but so far ive tried to make a ray around the character and get all parts within a 500 stud radius.

local PlrRay = Ray.new(hrp.CFrame.Position, hrp.CFrame.lookVector * 500)

while wait(1) do
	
	local hit, position = workspace:FindPartOnRay(PlrRay, char)
	
	print(hit)
	

end

This keeps printing nil, do you know what im doing wrong?

What I exactly meant is to cast multiple rays spreading out from a point. Your raycast is only one and seems to cast forward only and does not have any other directions.

Additionally, the raycast occurred once and you read the same ray over and over again.

What you can do is

local camera = workspace.CurrentCamera
local character = game.Players.LocalPlayer.Character
local part = character.HumanoidRootPart

local radius = 50

local extents = radius * Vector3.one
local min  = part.Position - extents
local max = part.Position + extents
local region = Region3.new(min, max)

local partsnearplayer = workspace:FindPartsInRegion3WithIgnoreList(region,{character,camera})
3 Likes

I like totally knocked out but im awake now and so far it seems like both methods work out pretty well, Im gonna go with the Region3 way in order to keep my code simple since im still really new the raycasting and making a bunch of rays seems like a hassle. Thanks for all the help guys :smile:

Are the parts static? What I mean is do they ever move? If they never move you can, at the start of the game, create a gigantic matrix and define the parts around a player by iterating over a specific region of the matrix.