How to make a "proper" Safe Zone system?

My game has a safezone that prevents pvp from happening. The system I made worked as intended but I dont think this is how other developers made their Safezone system.

image

Basically, when people touch the green part, their SafeZone bool value is set to true, and when they touch the red part, the bool is set to false. I dont really like my way of making safezone systems because it can be quite tedious to resize the zones.

Any better way of making a safezone system?

1 Like

You can use Touched and TouchEnded instead of making a seperate part that fires an additional Touched event.

Ive tried that before but it doesnt seem to work. Could you give me a code example?

These two articles have code examples for you.

Theres a little problem, it only works about 75% of the time. Sometimes when i enter the zone the touchended event fired last. Heres my code:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Safe") then
		print("hot")
		hit.Parent.Safe.Value = true
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Safe") then
		print("not hot")
		hit.Parent.Safe.Value = false
	end
end)

And this is the output when i enter the zone:
image

1 Like

I recommend you take a look into this module:

It uses rays and region3s, obviously you can take a peak at it’s source.

2 Likes

Well, i think this is your choice, for example, when i was about to make a war game, i just thought about make something like this, but i didn’t, other developers are using another system because they can modify it more easier, it depends about if that can be exploited or not, or using bad practices codes or not. This is your choice, and you can keep your system, because i think it’s good. But i recommend you keep the red walls with more distance from the green block. But i repeat, i recommend you keep your system. :+1:

2 Likes

Touched/UnTouched events are unreliable due to their nature of firing unpredictably and often sometimes never at all. These methods can also cause noticeable lag when complex geometries enter them, therefore it’s best to avoid them for situations like this.

I recommend using GetPartsInRegion3 for rectangles or a combination of that and raycasting for other shapes, as explained here.

1 Like

I was just about to suggest your thread, actually.

But for OP’s sake, I’d look to use Forever’s Region System if ya wanna do a safe zone type of thing.

Just used his module and it works as intended! :smile:

1 Like