Forcefield gets removed every time I shoot at somnebody in the safezone

Hi, IDK what’s going on but every time I shot at somebody in my datestone with a forcefield, it gets removed. This is the code I’m using currently… but I can’t come up with ideas.

local hitbox = script.Parent
hitbox.Touched:Connect(function(touch)
if touch.Parent:FindFirstChild(“Humanoid”) and touch.Parent:FindFirstChild(“Safezone Protection”) == nil then
local Forcefield = Instance.new(“ForceField”)
Forcefield.Name = “Safezone Protection”
Forcefield.Visible = true
Forcefield.Parent = touch.Parent
end
end)

hitbox.TouchEnded:Connect(function(touch)
if touch.Parent:FindFirstChild(“Humanoid”) and touch.Parent:FindFirstChild(“Safezone Protection”) ~= nil then
touch.Parent:FindFirstChild(“Safezone Protection”):Destroy()
end
end)

I would really appreciate some help, thank you!

2 Likes

Please, if you want to share some code use `````` :slight_smile:

my bad

local hitbox = script.Parent
hitbox.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") and touch.Parent:FindFirstChild("Safezone Protection") == nil then
		local Forcefield = Instance.new("ForceField")
		Forcefield.Name = "Safezone Protection"
		Forcefield.Visible = true
		Forcefield.Parent = touch.Parent
   end
end)


hitbox.TouchEnded:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") and touch.Parent:FindFirstChild("Safezone Protection") ~= nil then
		touch.Parent:FindFirstChild("Safezone Protection"):Destroy()
	end
end)
1 Like

Use Region3 instead since TouchEnded can be inaccurate sometimes.

1 Like

that helped out a lot, however, I’m struggling to try to detect when the player has left the zone to remove the forcefield. any help? thanks!

local Part = script.Parent
local Size = Part.Size
local Region = Region3.new(Part.Position-(Size/2),Part.Position+(Size/2))

while true do
	wait()
	local partsInRegion = workspace:FindPartsInRegion3(Region, nil,1000)
	for i, part in pairs(partsInRegion) do
		local char = part.Parent
		if part.Parent:FindFirstChild("Humanoid") ~= nil and char:FindFirstChild("Safezone Protection") == nil then
			local Forcefield = Instance.new("ForceField")
			Forcefield.Name = "Safezone Protection"
			Forcefield.Visible = true
			Forcefield.Parent = char
		end
	end
end

Well I fixed it with a bit of imagination haha. I said well why not mixing region 3 and touch ended? and there u have it… the perfect combination! thx for ur help!

UPDATE: I still find it kinda buggy… how can I detect when a player leaves a region3 area and do stuff? thx for ur help!

You can add a table of players that are currently inside the region3, for instance, every time you scan the region3 and find a player, you add the player to the table if they aren’t already. In future scans, check the table to see if the player in the table is still inside the region3, and if not, remove the forcefield. I don’t like TouchEnded very much since it can get buggy

yeah, I have checked it myself hahaha. If somebody can throw me a bit of light on this matter would be great! thx :slight_smile:

NVM, ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries did the job, thx y’all!!!