Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

how to detect players in a region3?

Asked by
hokyboy 203 Moderation Voter
4 days ago
local RegionHolder = workspace:GetChildren(workspace.RegionHolder)

while true do 
    wait(1)
    for i,part in pairs(RegionHolder,nil,1000) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
    print("Player Was Found In region")     
        end
        end
end

this is my current code it gives me no errors and i honstley have no idea why it wont work , Im new to region3

2 answers

Log in to vote
0
Answered by 4 days ago

You have to create a Region3 using Region3.new(start, end), then afterwards use workspace:FindPartsInRegion3(region, instance-to-ignore, max-parts-to-find)

local region3 = Region3.new(Vector3.new(0,0,0), Vector3.new(10,10,10))

while wait(1) do
local partsInRegion = workspace:FindPartsInRegion3(region3, nil, math.huge) --returns a table of parts in the region3
end
Ad
Log in to vote
0
Answered by 3 days ago

From an Region3 Expert ~

Making The Region3

First we need to create a region, iiMegabyteGaming's answer was OK, but it was missing an important. I'm sure you're region has already got an position you already gave it.

So let's make the region3, including position.

Before we begin, all this code must be in a Local Script.

local regionPart = workspace.Part
local newRegion = Region3.new(regionPart.Position-(regionPart.Size/2), regionPart.Position+(regionPart.Size/2))

Great! You made a region3, this will include position so you don't need to worry about that.

Detecting Parts in The Region3

Region3, is primarily made to detect any object within it's volume, we can use a built in function to get all the parts within the region we referenced.

workspace:FindPartsInRegion3(which region, parts to ignore, optional(max allowance of parts allowed in the region ) )

local regionPart = workspace.Part
local newRegion = Region3.new(regionPart.Position-(regionPart.Size/2), regionPart.Position+(regionPart.Size/2))

local runService = game:GetService("RunService")

local PartsToIgnore = {}


runService.RenderStepped:Connect(function()
    local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(newRegion, PartsToIgnore)

    for _, hit in pairs(partsInRegion) do
        if hit.Parent:FindFirstChild("Humanoid") then
            print("Player spotted in region:" .. hit.Parent.Name)

            -- any other code you want
        end
    end
end)

Now we've got the built in function we can start iterating through all the parts within the volume, if the part intersecting with the region contains a humanoid, we can start indicating the player is in the region.

Fun Minigame

King of the hill

At the end of each answer i make, i usually end with a fun thing you can make with it.

Put a sword to the starterpack Put the code i put below me into a LocalScript, I'll explain what the code does.

local regionPart = workspace.Part
local newRegion = Region3.new(regionPart.Position-(regionPart.Size/2), regionPart.Position+(regionPart.Size/2))

local runService = game:GetService("RunService")

local PartsToIgnore = {}
local playersInRegion = {}


runService.RenderStepped:Connect(function()
    local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(newRegion, PartsToIgnore)

    for _, hit in pairs(partsInRegion) do
        if hit.Parent:FindFirstChild("Humanoid") then
            local Character = hit.Parent

             local Hum = Character:FindFirstChild("Humanoid")

             if not table.find(playersInRegion, Hum) then

                    if (Hum ~= nil) then
                    table.insert(playersInRegion, Hum)
                else
                    table.remove(playersInRegion, Hum)
                 end
             end
         end
    end

    print(#playersInRegion)

    if #playersInRegion == 1 then
        regionPart.BrickColor = BrickColor.new("Bright green")
    else
        regionPart.BrickColor = BrickColor.new("Really red")
    end
end)

Invite some friends, and play king of the hill!

Answer this question