Why does touched event fire when i jump

So im trying to make a safezone but when i jump in the safezone, the “ZoneDisplay” event fires multiple times. Why?

local remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")

script.Parent.Touched:Connect(function(hit)
	local char = hit.Parent
	local player =  game:GetService("Players"):GetPlayerFromCharacter(char)
	local humanoid = char:FindFirstChild("Humanoid")
	
	if humanoid ~= nil and player and not char:FindFirstChild("ForceField") then
		local forcefield = Instance.new("ForceField")
		forcefield.Parent = char
		forcefield.Visible = false
		remotes.ZoneDisplay:FireClient(player, "Safe Zone")
	end 
end)

script.Parent.TouchEnded:Connect(function(left)
	local char = left.Parent
	local humanoid = char:FindFirstChild("Humanoid")
	
	if humanoid ~= nil and game:GetService("Players"):GetPlayerFromCharacter(char)and char:FindFirstChild("ForceField") then
		char:FindFirstChild("ForceField"):Destroy()
	end
end)

Touched fires anytime you’re touching the BasePart.
If you don’t want Touched to fire multiple times, you could set up a bool value that indicates if you’re in the safezone.

So when Touch is called the bool value will be set to true. Then the Touched Function can cancel if the value is true.
When TouchEnded is called, the bool value will be set to false meaning Touched will work again.

Idk if i did something wrong, but i tried that and it didn’t work.
here is the code

local remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local inSafeZone = false

script.Parent.Touched:Connect(function(hit)
	local char = hit.Parent
	local player =  game:GetService("Players"):GetPlayerFromCharacter(char)
	local humanoid = char:FindFirstChild("Humanoid")
	
	if humanoid ~= nil and player and not char:FindFirstChild("ForceField") and not inSafeZone then
		inSafeZone = true
		local forcefield = Instance.new("ForceField")
		forcefield.Parent = char
		forcefield.Visible = false
		remotes.ZoneDisplay:FireClient(player, "Safe Zone")
	end 
end)

script.Parent.TouchEnded:Connect(function(left)
	local char = left.Parent
	local humanoid = char:FindFirstChild("Humanoid")
	
	if humanoid ~= nil and game:GetService("Players"):GetPlayerFromCharacter(char)and char:FindFirstChild("ForceField") then
		char:FindFirstChild("ForceField"):Destroy()
		inSafeZone = false
	end
end)

Have you tried using a debounce like this?

local remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local inSafeZone = false
local debounce

script.Parent.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        local char = hit.Parent
	    local player =  game:GetService("Players"):GetPlayerFromCharacter(char)
	    local humanoid = char:FindFirstChild("Humanoid")
	
	    if humanoid ~= nil and player and not char:FindFirstChild("ForceField") and not inSafeZone then
	    	inSafeZone = true
			local forcefield = Instance.new("ForceField")
			forcefield.Parent = char
			forcefield.Visible = false
			remotes.ZoneDisplay:FireClient(player, "Safe Zone")
		end
		debounce = false
	end 
end)

script.Parent.TouchEnded:Connect(function(left)
	if not debounce then
		debounce = true
		local char = left.Parent
		local humanoid = char:FindFirstChild("Humanoid")
	
		if humanoid ~= nil and game:GetService("Players"):GetPlayerFromCharacter(char)and char:FindFirstChild("ForceField") then
			char:FindFirstChild("ForceField"):Destroy()
			inSafeZone = false
		end
		debounce = false
	end
end)
1 Like

i tried that but it still spammed and fired many times

1 Like

Have you considered using Region3 for Zone?

i’m not really sure what that is