Where is the Part located exactly? I think I may have an alternative solution if it’s located in a specific spot
Either way, you could also try to use the GetPartBoundsInBox() or GetTouchingParts() functions since Magnitude
is only reliant on a spherical type shape (Unless if you change the part to a sphere)
It depends on your use choice though, but say we wanna use the first function so we’d need to requite 3 parameters here:
- 1 - The Part’s Originated
CFrame
- 2 - How far we want the Box to go via a
Vector3
Size
- 3 - An
OverlapParams
Data Type which can specify specific parts you want it to touch/not touch
local Plrs = game:GetService("Players")
local Part = script.Parent
local Protected = {}
local Rate = 1
local TargetSize = 80
local PartParams = OverlapParams.new()
PartParams.FilterType = Enum.RaycastFilterType.Blacklist
PartParams.FilterDescendantsInstances = {Part}
while true do
local Parts = workspace:GetPartBoundsInBox(Part.CFrame, Vector3.new(TargetSize, TargetSize, TargetSize), PartParams)
table.clear(Protected)
for _, NearbyPart in pairs(Parts) do
local HRP = NearbyPart.Name == "HumanoidRootPart"
if HRP then
local Char = NearbyPart.Parent
local FFCheck = Char:FindFirstChildOfClass("ForceField")
table.insert(Protected, Char)
if FFCheck == nil then
local FFClone = Instance.new("ForceField")
FFClone.Parent = Char
end
end
end
for _, Plr in pairs(Plrs:GetPlayers()) do
local Char = Plr.Character
if Char then
local FFCheck = Char:FindFirstChildOfClass("ForceField")
if FFCheck and not table.find(Protected, Char) then
FFCheck:Destroy()
end
end
end
task.wait(Rate)
end
There’s a lot of that you may see here, but let’s go ahead and break it down:
Lines 1-6:
local Plrs = game:GetService("Players")
local Part = script.Parent
local Protected = {}
local Rate = 1
local TargetSize = 80
These are just our variables for easier definition & obtaining, nothing much here
Lines 8-10:
local PartParams = OverlapParams.new()
PartParams.FilterType = Enum.RaycastFilterType.Blacklist
PartParams.FilterDescendantsInstances = {Part}
Now these are what’s known as a new type, the OverlapParams which can create a specific boundary provided via the Origin’s CFrame, and overall Size you want it to tend
-
FilterType
defines the type you want the Params to be: Blacklist will ignore any Parts within the boundary, and Whitelist only detects Parts within that boundary
-
FilterDescendantsInstances
checks for what parts you want to Find/Ignore provided by the FilterType
Now moving on, inside of our loop here:
Lines 13-33
local Parts = workspace:GetPartBoundsInBox(Part.CFrame, Vector3.new(TargetSize, TargetSize, TargetSize), PartParams)
table.clear(Protected)
for _, NearbyPart in pairs(Parts) do
local HRP = NearbyPart.Name == "HumanoidRootPart"
if HRP then
local Char = NearbyPart.Parent
local FFCheck = Char:FindFirstChildOfClass("ForceField")
table.insert(Protected, Char)
if FFCheck == nil then
local FFClone = Instance.new("ForceField")
FFClone.Parent = Char
end
end
end
A whole lot, but if we simplify everything it should be taken quite easily here
Parts
is what we use the GetPartBoundsInBox()
function for, and referencing its parameters return back a Table of all the parts back within that Part Boundary
We also wanna go ahead and clear our Protected
table using table.clear
so that we can refresh which Character Models are exactly safe and not
Next, we use a pairs loop to go through that said Parts
table and check for a specific Part Name, preferably the HumanoidRootPart
and if we’re able to find one, we can create variables for both the Character, and a check for a ForceField
inside that said Character!
We also want to insert a Character inside our Protected
function, so we use table.insert
for that
Then if our FFCheck
variable returns back nil
for the ForceField, then we can create one for our safe Character here
Lines 35-45
for _, Plr in pairs(Plrs:GetPlayers()) do
local Char = Plr.Character
if Char then
local FFCheck = Char:FindFirstChildOfClass("ForceField")
if FFCheck and not table.find(Protected, Char) then
FFCheck:Destroy()
end
end
end
Now, a little additional stuff I added here is just simply to check for all Players using the GetPlayers()
function, and we want to reference the Character yet again so that we can both check for the ForceField
if any, and if they’re protected or not using the table.find()
function
If both of our conditional statements are met, then we can remove that Forcefield as they’re not close in the safe zone And hopefully, you’ll end up with something like this as the result!
Hopefully this helps you a bit on how these unique functions work, cause they’re really handy in situations like these
Here’s also the place file in case you wanna try it out for yourself:
SafeZonePlace.rbxl (33.3 KB)