If you are making a safe zone, then perhaps Region3?
Here is a script I just wrote. I wasn't in studio, so feel free to test it.
The general idea is here, so have a go.
Before damaging the player, you can check if they are in the region3.
Example
We are going to have a singular function to handle all of the damage.
This makes it so we only need to write the region3 check once.
01 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local remoteEvent = replicatedStorage:WaitForChild( "RemoteEvent" ) |
06 | local min = part.Position - ( 0.5 * part.Size) |
07 | local max = part.Position + ( 0.5 * part.Size) |
08 | local region = Region 3. new(min, max) |
12 | local function damageTarget(humanoid, enemy, amount) |
13 | if humanoid = = nil then return end |
14 | if enemy = = nil then return end |
16 | local partsInRegion 3 = workspace:FindPartsInRegion 3 (region) |
20 | for _, parts in pairs (partsInRegion 3 ) do |
21 | if parts.Parent = = enemy then |
27 | if found then return end |
29 | humanoid:TakeDamage(amount) |
34 | RemoteEvent.OnServerEvent:Connect( function (player) |
35 | local character = player.Character |
37 | local projectile = Instance.new( "Part" ) |
38 | projectile.Size = Vector 3. new( 2 , 2 , 2 ) |
39 | projectile.CanCollide = false |
40 | projectile.BrickColor = BrickColor.new( "New yeller" ) |
41 | projectile.CFrame = character.HumanoidRootPart.CFrame |
43 | local newVelocity = Instance.new( "BodyVelocity" ) |
44 | newVelocity.MaxForce = Vector 3. new( math.huge , math.huge , math.huge ) |
45 | newVelocity.Velocity = (projectile.CFrame.lookVector * 50 ) |
46 | newVelocity.Parent = projectile |
48 | projectile.Parent = workspace |
50 | local hitCooldown = { } |
52 | projectile.Touched:Connect( function (hit) |
53 | if hit:IsDescendantOf(character) then return end |
54 | if hit.Parent:FindFirstChildOfClass( "Humanoid" ) then |
55 | if hitCooldown [ hit.Parent ] = = nil then |
56 | hitCooldown [ hit.Parent ] = true |
58 | local enemy = hit.Parent |
59 | local humanoid = enemy:FindFirstChildOfClass( "Humanoid" ) |
60 | damageTarget(humanoid, character, 25 ) |
66 | game:GetService( "Debris" ):AddItem(projectile, 4 ) |
For more information, check here for region3
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3