Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
SuperFryX 130
8 years ago

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

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

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.)

Ad

Answer this question