Simply put, this is a script that create a boundary region, where as long as player is inside this region. They’re safe from any damage until they leave the region.
At first, i tried to use a non-collideable part surrounding the boundaries to check whenever the player leave or enter the region. But i find it too much of a hassle and less flexible. Then i turn to use region3, using past experience of 2 year ago, hopefully also train my skill of using region3. Whoops, its already deprecated nowaday (Wonder why). And then i heard there’s the new region3 called WorldRoot:GetPartBoundsInBox()
-
Is there any way i can improve this already working code? or a suggestion of optimization? If possible i want to look for a way that the code only run whenever a new part entered the region, so i dont have to constantly checking. But i dont think this code is performance heavy.
-
Also i have issue regarding blacklisting part that are inside the region but are native to it. Like a building, the script below doesn’t properly blacklist the part. Anyone know why?
local Players = game:GetService("Players")
-- Region constructor
local boxPos = CFrame.new(1, 19, 14)
local boxSize = Vector3.new(32, 16, 32)
-- Blacklisting the part native to the region
local blacklist = workspace.SpawnArena:GetChildren()
local params = OverlapParams.new(blacklist,Enum.RaycastFilterType.Exclude)
-- Table for storing player
local protectedplayer = {}
local function createForcefield(parent)
local f = Instance.new("ForceField")
f.Parent = parent
return f
end
while true do
-- Yes. This constantly check.
local Parts = game.Workspace:GetPartBoundsInBox(boxPos,boxSize,params)
for i,part in pairs(Parts) do
local model = part.Parent
if model:FindFirstChild("Humanoid") and Players:GetPlayerFromCharacter(model) and not model:FindFirstChild("ForceField") then
-- Insert the player character to a table to track whoever inside the region. And later to track who left the region
if not protectedplayer[model.Name] then
protectedplayer[model.Name] = model
local forcefield = createForcefield(model)
end
end
end
for i,player in pairs(Players:GetChildren()) do
-- Look for every player character to crosscheck who's inside the region with the outsider
local character = player.Character
if not character then continue end
if not table.find(Parts,character.HumanoidRootPart) and protectedplayer[character.Name] then
-- Destroy forcefield for player outside boundaries
if character:FindFirstChild("ForceField") then
character.ForceField:Destroy()
protectedplayer[character.Name] = nil
print("huzzah")
end
end
end
task.wait()
end