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

How do I get players in a Region3 area?

Asked by
trecept 367 Moderation Voter
5 years ago

I'm very new to Region3 stuff, I've never used or seen it before personally. I'm using it as I think it's a very good unexploitable system on detecting whether a player is in a lobby or in a map to change their health, but I'm wondering how can I find players who enter a Region3 area, so I can then change their maxhealth to math.huge?

0
Please try reading up on Region3 first, experimenting with it, and then asking questions regarding it if you're having any trouble (with code also provided). That is the best way we will be able to help you. nilVector 812 — 5y
0
local a = Region3.new(Vector3.new(1, 1, 1), Vector3.new(10, 10, 10)) This is as far as I've gotten, I've read wiki but I don't understand how to properly get players inside the area? trecept 367 — 5y
0
I have answered your question below. nilVector 812 — 5y

1 answer

Log in to vote
2
Answered by
nilVector 812 Moderation Voter
5 years ago

The workspace has a function called FindPartsInRegion3, which takes a Region3 object to do as it name implies: find the parts in that Region3.

As you mentioned earlier, you have:

local a = Region3.new(Vector3.new(1, 1, 1), Vector3.new(10, 10, 10))

Now, what you need to do is make use of the FindPartsInRegion3 function of workspace to retrieve all the parts in a. For each part found, you need to determine if the part belongs to the character of a player or not. It can be done so like this:

local players = {}
local a = Region3.new(Vector3.new(1, 1, 1), Vector3.new(10, 10, 10))

for _, part in pairs(workspace:FindPartsInRegion3(a, nil, math.huge)) do
    local character = nil
    -- In the case the part is a limb
    if part.Parent:FindFirstChild("Humanoid") then 
        character = part.Parent
    -- In the case the part is part of an accessory
    elseif part.Parent.Parent:FindFirstChild("Humanoid") then
        character = part.Parent.Parent
    end

    if character then
        local player = game.Players:GetPlayerFromCharacter(character)
        if player and not players[player.Name] then
            players[player.Name] = player
        end
    end
end

Now, the players table will contain all the Players found in the specified Region3, with the key being the Player's name and the value being the Player object.

0
Thank you for the help, one more question: If I wanted to know when a player entered and leaved a Region3, would using the FindPartsInRegion3 function in a while wait() loop lag my game if over 100 parts are in the region? trecept 367 — 5y
1
At the expense of that, I would rather just have an invisible part with CanCollide set to false and its size/position being the same as the Region3. Then, utilize the Touched and TouchEnded events of the part to find a player that enters or leaves the "region" nilVector 812 — 5y
0
This was what I was going to do, but exploiters can exploit this by e.g bringing the part to their position. Regions are something exploiters cannot "bring" though (I assume) trecept 367 — 5y
1
Yes, that is true. One way to get around this would be to check (from the server) to see if the part's position and size is equal to what you set it to be whenever a player enters/leaves the part. nilVector 812 — 5y
Ad

Answer this question