How do I make it so a player can't take damage while in a certain area? (Inside a part)

ForceField | Documentation - Roblox Creator Hub ForceFields can prevent the humanoid from taking damage. If you create a loop which checks to see if the Player is inside of the safezone, then you can give the player immunity (from the TakeDamage function) by inserting a safezone into their Character.

while true do
    wait(1)
    if isInSafeZone(player1) and not playerHasFF then
        --give forcefield
        playerHasFF = true
    elseif not isInSafeZone(player1) and playerHasFF then
        -- remove the force field
        playerHasFF = false
    end
end
4 Likes

That is only with the :TakeDamage() function. Manually setting the health will still work with a forcefield.

@AtomicTacoDev, keep that in mind if you are using this solution.

1 Like

Err, I doooon’t think I’ll be manually setting the health, so it should be fine, thanks for the heads up =)

1 Like

if you are dealing with weapons, then you can have an if statement detecting if a value is true or not(the value is true if the rootpos is in the safe zone)

1 Like

Heartbeat → Remote Event. Store player states on the server. Send an event if the player is in the same region as the part specified. If true, send to the server via remote event that player state is non-pvp. Interchange if false.

I’m trying to make a safezone, when a player is inside it, they can’t take damage. I’ve tried looking on the internet but kept running into problems, so at last I’m making this post.

1 Like

There are a few ways you can do this.

You can check if a point is inside a sphere of radius r, by comparing the distance from the center to player to r. If the distance is less than r, the player is inside the radius.

You can use Region3 to check if the character’s HumanoidRootPart is inside an axis aligned bounding box, meaning you can only check regions that are cube- like in shape and have no rotation.

You can use the RotatedRegion3 ModuleScript to do the same thing, but with Region3s that can be rotated. The same module also lets you check if a point is inside a Sphere, and helps with easily making regions that correspond to parts.

If you need these “safe zones” to have more complicated shapes, you can check against several regions, and see if the player is in any of those.

simplest way, is just use a part and use the ontouch event

script.Parent.OnTouched:Connect(function()
–do something
end)

Firstly, it’s Touched - not OnTouched.

Secondly, Touched only fires when another part starts touching it could be anything other than a player so you’d need to use GetPlayerFromCharacter to check if it’s really a player.

Thirdly, the event to go with this - TouchEnded - fires when the touch between the two parts ends, this may be because the player is moving or jumping.

Touched really isn’t the way to go about areas, region3 as ThanksRoBama suggested or magnitude checks are much better even if they’re ineffecient in cases.

I work with 3 other engines. And, im not looking to spoon feed anyone. just offer some help. A bit of research would have told them that too :slight_smile:

also, if done right, touched is all they would need.

here is another article on it, using regions.

Use magnitude with a loop to see if the player is near the safezone.

I’d suggest using Touch and TouchEnded where if touched you give them forcefield and touchended is remove.

If that doesn’t work you could use RunService then calculate the distance between the player and a safezone area if your safezone is a cirlce or square.

1 Like

‘.Touched’ would be a terrible way to solve the problem, no offense.

2 Likes
local RelativePosition = SafeArea.CFrame:Inverse() * Root.Position
local Size = SafeArea.Size/2
local X,Z = math.abs(RelativePosition.X),math.abs(RelativePosition.Z)
if X < Size.X and Z < Size.Z then
    print("Safe")
end

–can loop thru players:GetPlayers()

1 Like

Personally I’d use region3, so something like this maybe

--making the position using a part
pos1,pos2 = Vector3.new(game.Workspace.SafeZone.Position - game.Workspace.SafeZone.Size / 2), Vector3.new(game.Workspace.SafeZone.Position + game.Workspace.SafeZone.Size / 2)
SafeZone = Region3.new(pos1,pos2)
while true do wait()
PartsInSafeZone = game.Workspace:FindPartsInRegion3(SafeZone,game.Workspace.SafeZone)
humanoid = PartsInSafeZone.Parent:FindFirstChild("Humanoid")
if humanoid then
	--checking to see if a player is in this part
		--For example if there were a bool value in the player that represents if the player is in the safezone or not then we'd set that to true
			plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			plr.Safe = true
	end
end

Then make that turns plr.Safe to false whenever the player leaves the region

1 Like

region3 isnt really performance friendly, I would suggest going with my method which is a point to plane collision system it also works with rotation because its dealing in local space(relative)

How is touched not good? I made 2 examples in under 30 minutes. You can check it out under @Paradigm_Dinosaurs.

1 Like

Touched tells you that a part has entered the area - and nothing else. A player can enter the safe zone once and never become vulnerable again.

The converse to the Touched event is the TouchEnded event. You can detect characters entering the region with Touched and remove their forcefield when they trip TouchEnded with their base part. This would be the ideal, most performant solution if it weren’t so easy to

  1. get wrong (e.g any body part leaving the region means the entire player left, or poorly keeping track of which parts are touching the part at the moment);
  2. exploit, at least in my mind. There’s got to be something the client can do to keep one foot just barely inside the safe zone when it’s actually really far away.

Edit: I didn’t skim the thread deep enough and assumed from your first post that you were clueless about TouchEnded.

also, if done right, touched is all they would need.

Very true. When the player is found entering another region, that must mean they are not in any other regions. At this point, you can note which regions are adjacent to which, and set up simple sanity checks/assertions for the player’s movement - are they phasing through walls? flying? etc. This is useful in any place.

As for every other post here, regions are a simple and foolproof way to accomplish what OP wants — until he uses too many of these safezones in one place, each of which is checking for every single player in the place…

My example allows for addition. With your concern to players hacking. That would be controlled separately.

My example was merely created to say how many players are in safe zones, but also stores who is in a safe zone, by use of touched. With that, i can do any further checks if necessary.

And your right regions will cramp your style eventually. This runs on an event system.

Just in case anyone was interested, I did some updates to the safe zone system I was working on. It now displays what zone all players are in (if in a zone). You basically just add a part, and you have a safe zone. as many as necessary, working on an event system.

1 Like