Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I create a table of players within a certain distance to a part?

I am making a turret that I want to target random individuals within a certain distance and not just the closest player. I'm having trouble starting it though as I have never really worked much with distances before and such, so I'm completely clueless on this. So basically, how would I create a table of players within a certain distance to a part?

1 answer

✓ Accepted answer

You can use DistanceFromCharacter to get the distance from the character's head.

local turret -- define turret
local radius=20

local list={}
for _,v in pairs(game.Players:GetPlayers())do
	local dist=v:DistanceFromCharacter(turret.Position)
	if dist~=0 and dist<radius then
		list[#list+1]=v
	end
end

DistanceFromCharacter will return 0 if the distance cannot be found for some reason (no head/no character.)

This discussion is preserved as it was. New answers, comments, and votes are closed.