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

How to get all Players within a certain range of a Part?

Asked by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

Hey all! I apologize if this question has been asked before, because I remember seeing it a while back (I never found it), but how would I detect all Players within a certain range? Now, I know your first response would be to make a while loop, then a for loop that goes through all players, and checks if they're near the part. That would be helpful, but I was wondering if there was an alternate way, possibly more efficient? I haven't attempted the script, because I do not know of a method that would accomplish this task. Any tips? Thanks all!

0
I do not believe there is such a way. ;-; Redbullusa 1580 — 9y

1 answer

Log in to vote
3
Answered by 9 years ago

I would use DistanceFromCharacter() or magnitude. To see if all the players are in the place, I would use a loop. Or use Heartbeat or Renderstepped.

local part = workspace.Part
local thing = 0

function activate()
    --Code goes here
end

game:GetService("RunService").RenderStepped:connect(function()
    for _,p in pairs(game:GetService("Players"):GetPlayers()) do
        if p:DistanceFromCharacter(part.Position) then
            thing = thing+1
            if thing >= game:GetService("Players").NumPlayers then
                activate()
            end
        end
    end
end)

This is the best I can think of... Anyway... Hope it helps!

0
Hmm, thanks! I was hoping of some other way, but this is a new way. Shawnyg 4330 — 9y
Ad

Answer this question