What is the better way to detect If a player enters zones?

Greetings, developers. I have done this pvp and safe zone. This script just checks If a player touched part and if so It activates pvp but this does not work very well. It sometimes works and sometimes does not. Here is a video:Imgur: The magic of the Internet

Sorry for the low quality, I recorded it in the studio

The Script

local PM = game.Workspace.Arena.Give
local SZ = game.Workspace.Arena.remover.Remover
local UN = game.Workspace.Arena.Untouch

game.Players.PlayerAdded:Connect(function(plr)
	local PVP = Instance.new("BoolValue")
	PVP.Name = "Isworking"
	PVP.Value = false
	PVP.Parent = plr
end)

UN.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Isworking.Value == true then
			wait(1)
			plr.Isworking.Value = false
		end
	end
end)

SZ.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Isworking.Value == true then
			wait(1)
			plr.Isworking.Value = false
		end
	end
end)

PM.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Isworking.Value == false then
			wait(1)
			plr.Isworking.Value = true
		end
	end
end)
1 Like

There is already a thread on this subject, you can find you answer here: Whats the best way to go about region detection? - #6 by matt1020304050

As said, .touched does not always fire depending on the situation, therefore there are some other methods which are more reliable in detecting a collision with the player.

Although if you’re looking for the best solution only for entering or exiting regions, .touched can be used as long as the part which you are trying to detect it on is large enough for it to guarantee that a player who walks through it will fire the .touched signal. Otherwise you’d have to continuously check a player’s region, which can be done with some of the solutions mentioned in the linked thread.

1 Like

I use a module called ZonePlus. It’s simple and easy to use. It consists of 2 main functions but there’s more of them. A lot of people use it and recommend it and it’s very accurate. Here’s the link: ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries

3 Likes

I suggest using ZonePlus as @BandQueenForever suggested as it is really easy to use with super helpful documentation and is what I use whenever trying to make a hitbox or zone

4 Likes