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)
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!