Hello Developers!
I don’t understand the purpose of region3 and how to use it. The devhub isn’t much of a help. I don’t understand functions like :FindPartsInRegion3
and someone use cases of when I’ll use region3. Any help is appreciated!
Hello Developers!
I don’t understand the purpose of region3 and how to use it. The devhub isn’t much of a help. I don’t understand functions like :FindPartsInRegion3
and someone use cases of when I’ll use region3. Any help is appreciated!
You can use Region3 to detect parts inside, as the name says, a region which you can define with a start and an end point. You can imagine it as a square-like shaped invisible part, which you can use for your own purposes. Some people use it for melee combat systems, other to create music zones, others to teleport a group of players inside a region to another place. There really are a lot of things you can use it for.
To detect the parts inside of a region you can use, like you said, :FindPartsInRegion3
. There also are alternatives which use tables, such as FindPartsInRegion3WithWhiteList
,which ignores every basepart that you didn’t put inside the table, or FindPartsInRegion3WithIgnoreList
which ignores every basepart inside the table.
An awesome module you can use when you’ve learn the basics of Region3s is the RotatedRegion3 module by EgoMoose, which allows you to create rotated regions even with different shapes (block, wedge, cylinder, sphere, etc…).
Region3s however might have a downside on performance, especially if you use it on the server, when they’re big. Obviously, it depends on your game. If you have a simple game, like an obby, using some Region3s surely won’t impact at all the performance of the game.
Wow! It’s really useful. But can you give me a code example of the function :FindPartsInRegion3
?
Sure! So let’s say you’ve got a 4x4x4 part and you want to detect what’s inside of it. Let’s do it step-by-step:
local part = Instance.new("Part",workspace)
part.Size = Vector3.new(4,4,4)
part.Anchored = true
part.CanCollide = false
local min = part.Position - (0.5 * part.Size) -- Let's call this "A" point
local max = part.Position + (0.5 * part.Size) -- Let's call this "B" point
local region = Region3.new(min, max) -- We actually create the region here, based on the A and B points
local parts = workspace:FindPartsInRegion3(region, part, 20) -- With this, we're detecting 20 parts inside of the region, except the actual part we're using as zone, which we've put inside of the ignore argument
for i,v in pairs(parts) do
print(v.Name) -- prints the name of every part found
end
Other examples
for i,v in pairs(parts) do
if v.Parent:FindFirstChild("Humanoid") then
v.Parent.Humanoid:TakeDamage(5)
end
end
for i,v in pairs(parts) do
if game.Players:GetPlayerFromCharacter(v.Parent) then
local Plr = game.Players:GetPlayerFromCharacter(v.Parent)
if Plr.Character == nil then
return
end
Plr.Character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,0))
end
end
Unfortunately, there’s a 20 max detectable parts limit by default. However, if you use the module I gave you, you can go much higher (even 1000 will work). Of course the higher, the worse the performance will be.
These are just some basic examples to let you understand how to use it.
Thanks for clearing my confusion. But it doesn’t work and I go inside the part, it won’t kill me
Oh obviously once you create it, it won’t have a time limit which it lasts. You would have to use a loop to keep creating it. Sorry should’ve mentioned that.