Why does this script not add a forcefield to a player?

Im creating a no pvp zone. For some reason this script dose not work. the script’s parent is StarterCharacterScripts.
the script:

script.Parent.Torso.Touched:Connect(function(hit)
	script.Parent:WaitForChild("Torso")
	if hit.Parent:FindFirstChild("IsASafeSone") then
		Instance.new("ForceField",script.Parent)
	end
end)

script.Parent.Torso.TouchEnded:Connect(function(hit2)
	if hit2.Parent:FindFirstChild("IsASafeSone") then
		script.Parent:WaitForChild("ForceField")
		script.Parent.ForceField:Destroy()
	end
end)

Maybe try checking if there is already a forcefield. Also, your script is saying when the torso is hit. Maybe change the torso to the char’s left or right leg as they touch mostly everything.

Could you give us a little more context whereas to the hierarchy of the safe zone and such?

Alright. So i just found a problem but i cant seem to find the solution. i put this code in:
script.Parent.Torso.Touched:Connect(function(h)
print(h.Parent.Name)
end)
and it said that the player touched workspace.

script.Parent["Left Leg"].Touched:Connect(function(hit)
	script.Parent:WaitForChild("Left Leg")
	if hit.Parent:FindFirstChild("IsASafeSone") then
if script.Parent:FindFirstChildOfClass("ForceField") then
else
		Instance.new("ForceField",script.Parent)
end
	end
end)

script.Parent.Torso.TouchEnded:Connect(function(hit2)
	if hit2.Parent:FindFirstChild("IsASafeSone") then
		script.Parent:WaitForChild("ForceField")
		script.Parent.ForceField:Destroy()
	end
end)

In that case maybe try using:

local inasafezone = false

if h:FindFirstChild("IsSafeSoze")
inasafezone = true
end

ok ill try that‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎

but i addded

if hit.Parent:FindFirstChild("IsASafeZone") then

so it can clasify if its a safezone

local SafezonePart = script.Parent
local FFLeaveDelay = 1.25 -- Delay before removing the forcefield
local FFVisible = true -- ForceField Visibility


local InField = {}
script.Parent.Touched:Connect(function(t)
	local Player = game.Players:GetPlayerFromCharacter(t.Parent)
	if Player ~= nil and t.Name == "HumanoidRootPart" then
		if InField[Player.UserId] then return end
		InField[Player.UserId] = true
		local Character = Player.Character or Player.CharacterAdded:Wait()
		if Character ~= nil then
			local Forcefield = false
			for i,v in pairs(Character:GetChildren()) do
				if v:IsA("ForceField") then
					Forcefield = true
					break
				end
			end
			if not Forcefield then
				local FF = Instance.new("ForceField", Character)
				FF.Visible = FFVisible
				FF.Name = "ForceField"
			end
		end
	end
end)


script.Parent.TouchEnded:Connect(function(t)
	local Player = game.Players:GetPlayerFromCharacter(t.Parent)
	if Player ~= nil and t.Name == "HumanoidRootPart" then
		InField[Player.UserId] = false
		local Character = Player.Character or Player.CharacterAdded:Wait()
		if Character ~= nil then
			for i,v in pairs(Character:GetChildren()) do
				if v:IsA("ForceField") then
					spawn(function()
						wait(FFLeaveDelay)
						if not InField[Player.UserId] then
							v:Destroy()
						end
					end)
				end
			end
		end
	end
end)

ff.rbxl (19.1 KB)