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.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
local min = part.Position - (0.5 * part.Size)
local max = part.Position + (0.5 * part.Size)
local region = Region3.new(min, max)
local function damageTarget(humanoid, enemy, amount)
if humanoid == nil then return end
if enemy == nil then return end
local partsInRegion3 = workspace:FindPartsInRegion3(region)
local found = false
for _, parts in pairs(partsInRegion3) do
if parts.Parent == enemy then
found = true
break
end
end)
if found then return end
humanoid:TakeDamage(amount)
end
RemoteEvent.OnServerEvent:Connect(function(player)
local character = player.Character
local projectile = Instance.new("Part")
projectile.Size = Vector3.new(2, 2, 2)
projectile.CanCollide = false
projectile.BrickColor = BrickColor.new("New yeller")
projectile.CFrame = character.HumanoidRootPart.CFrame
local newVelocity = Instance.new("BodyVelocity")
newVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
newVelocity.Velocity = (projectile.CFrame.lookVector * 50)
newVelocity.Parent = projectile
projectile.Parent = workspace
local hitCooldown = {}
projectile.Touched:Connect(function(hit)
if hit:IsDescendantOf(character) then return end
if hit.Parent:FindFirstChildOfClass("Humanoid") then
if hitCooldown[hit.Parent] == nil then
hitCooldown[hit.Parent] = true
local enemy = hit.Parent
local humanoid = enemy:FindFirstChildOfClass("Humanoid")
damageTarget(humanoid, character, 25)
end
end
end)
game:GetService("Debris"):AddItem(projectile, 4)
end)
For more information, check here for region3
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3
2 comments
What weapons are you using? You can make them do 0 damage if the player is in the safe zone.
@srimmbow so im guessing a boolean value will be used for this, I'm just doing some preparing until I add my punch tool to my weight lifting game, I guess I could do that. Make a int value for the damage?