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