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

Quick Explanation on Region3?

Asked by
StoIid 364 Moderation Voter
7 years ago
Edited 7 years ago

I know there are about 3 ways to see if something is near another thing (lol)

-Magnitude

-RayCasting

-Region3

I was wondering if someone could give me a brief explanation on Region3 and how it works before i dive into the very confusing wiki and start experimenting with it.

2
I hope you get a good answer because the wiki article has always kind of confused me Perci1 4988 — 7y

1 answer

Log in to vote
4
Answered by 7 years ago
Edited 7 years ago

Region3 is basically a section of the world, as shown by the diagram at http://wiki.roblox.com/index.php?title=API:Region3 . It's basically a representation of location and size of a three-dimensional area, from one corner to its opposite furthest corner.

Using Region3 basically means you look into that area of the Workspace, e.g. looking for parts within it using the FindPartsInRegion3 method of game.Workspace.

Example Usage

Say I wanted to find players within a certain area, indicated by a brick. Basically, we want every player that is inside that non-CanCollide part. We could do this by using a Region3:

function CreateRegion3FromPart(Part)
    return Region3.new(Part.Position-(Size/2),Part.Position+(Size/2))
end

function GetPlayersInPart(part)
    local region = CreateRegion3FromPart(part)
    local partsInRegion = workspace:FindPartsInRegion3(region,nil,math.huge)
    local Players = {}
    for _,Part in pairs(partsInRegion) do
        local player = game.Players:GetPlayerFromCharacter(Part.Parent)
        if player then
            table.insert(Players,player)
        end
    end
    return Players
end

GetPlayersInPart(game.Workspace.Model.Part)

Your Context You could use the code above somehow, to get players within an area surrounding the "another thing". Magnitude is the best for distance as it uses vector mathematics, it's far better than region3: Region3s are best for searching specific areas or using specific areas for things such as filling in terrain. Raycasting (the c is lowercase) is utterly pointless in finding the distance between a player and the thing, as it requires a direction and is based around the idea of travelling into a vector until it hits a collidable object. Consider using the DistanceFromCharacter method of player for calculating distance from a thing, this does the magnitude for you! It returns nil if the character is nil, otherwise the magnitude.

0
Even though this is year old just wanted to point out that this would insert a ton of player values because a character has multiple parts in it. Wafflecow321 457 — 6y
0
Even though this is year old just wanted to point out that this would insert a ton of player values because a character has multiple parts in it. Wafflecow321 457 — 6y
Ad

Answer this question