ForceField Part

How do I make it so that when a player touches a specific part it gives them a forcefield (that makes them invincible), and when the PLAYER STEPS OUT OF THE PART it takes off their forcefield.

3 Likes

Here’s some sample code. Check out force fields to learn more.

Put this Script under the part. I recommend making it Anchored = true, Transparency = 1, and CanCollide = false, and at least 2 studs tall (like an invisible field) or it will be buggy.

local part = script.Parent -- Or another part

part.Touched:Connect(function(part)
	local character = part.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then
	 	 return
	end

	local forceField = character:FindFirstChild("PartForceField")
	if forceField then -- If it already exists
		return
	end
	forceField = Instance.new("ForceField")
	forceField.Name = "PartForceField"
	forceField.Parent = character
end)

part.TouchEnded:Connect(function(part)
	local character = part.Parent
	-- The humanoid part is not technically necessary, just to make sure it is a character
	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then
	 	 return
	end
	for _, touchingPart in ipairs(part:GetTouchingParts()) do -- See if any other body parts are currently touching; if so; don't destroy it.
		if touchingPart:IsDescendantOf(character) then
			return
		end
	end
	local forceField = character:FindFirstChild("PartForceField")
	if forceField then
		forceField:Destroy()
	end
end)

Also, make sure whatever script causes the Humanoid to loose health, is using Humanoid:TakeDamage(), otherwise it might not work properly.

Is there a way I can make the player not take damage without using forcefields? BTW I’m making a sword fight game and want a safezone.

You can probably just set ForceField.Visible to false, and you won’t see it at all. It also protects against explosions, so if you add bombs or something, this will work much better.

	forceField = Instance.new("ForceField")
	forceField.Name = "PartForceField"
	forceField.Visible = false
	forceField.Parent = character

If that doesn’t solve it for you:

Under your safe zone part, put this:

-- Allow part to compute collisions even though it has CanCollide set to false
part.Touched:Connect(function() end)

-- Allow the code to know this is a safe zone
part:AddTag("SafeZone")

In your sword script, modify the section where damage is given to look like this.

-- In this context:
-- otherPart is a part of the character that the sword hit
local character = otherPart.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
	return -- No humanoid; it's not a character
end

-- Check if other limbs are touching any SafeZone part
for _, otherLimb in ipairs(character:GetChildren() do
	if not otherLimb:IsA("BasePart") then
		continue
	end
	for _, touchingPart in ipairs(otherLimb:GetTouchingParts()) do
		if touchingPart:HasTag("SafeZone") then
			return -- Cancel damage if they're in a safe zone
		end
	end
end

humanoid:TakeDamage(20) -- Or humanoid.Health -= 20