Zone Capture System

Recently, I’ve been trying to create a zone capture system which lets a team capture a zone by simply going on a specific platform/base part. I’ve tried approaching this goal by setting up a base part that involves Touch and Touch Ended Events but that was problematic since touch ended events fired when a character was still on the base part.

For example:


I want the zone to be a certain color and that player to be added into a table whilst the character is on the platform.

For more reference, here’s the code I attempted to make:

local plrsInsideZone = {}
script.Parent.Touched:Connect(function(hit)                         --Player goes inside Zone
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr and not findPlrInZone(plr.Name) then
		table.insert(plrsInsideZone,plr.Name)
		print("inserting "..plr.Name)
		print(table.concat(plrsInsideZone))
	end
end)
script.Parent.TouchEnded:Connect(function(hit)						--Players goes outside Zone
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local pos = findPlrInZone(plr.Name)
	if pos then
		table.remove(plrsInsideZone,pos)
		print("removing "..plr.Name)
		print(table.concat(plrsInsideZone))
	end
end)	
function findPlrInZone(plr)										--Checks if player is inside Zone
	for i,v in ipairs(plrsInsideZone)do
		if v == plr then
	               return i
		else
			return false
		end
	end 
 end

Any ideas on how to achieve my goal of a capture zone system?

3 Likes

Touched/TouchEnded have been pretty unreliable in general for humanoids. I would recommend something like checking every frame which players are in the area of the zone.

local function isPointInZone(point, zonePart)
    local relPoint = zonePart.CFrame:PointToObjectSpace(point)
    return math.abs(relPoint.X) < zonePart.Size.X * 0.5 and math.abs(relPoint.Z) < zonePart.Size.Z * 0.5
end

local playersInZone = {}
local function onHeartbeat(dt)
    for i, player in pairs(game.Players:GetPlayers()) do
        if player.Character and player.Character.PrimaryPart and isPointInZone(player.Character.PrimaryPart.Position, script.Parent) then
               playersInZone[player.UserId] = true
        else
              playersInZone[player.UserId] = nil
        end
    end
end

Before you worry about the performance of this, though, take a moment to consider that computers are actually pretty fast, even when running Lua. Fix it when it becomes a problem, because by then, you will probably have learned enough to do it well.

4 Likes

I think the best method is Region3, they are easier to use and with their function you can achieve what you want.

1 Like

Thank you for replying, I’ll try to implement this into my system

Thanks for replying, I’ll try out region 3 too

1 Like

I would avoid Region3. It has a lot more overhead and false positives because it uses axis-aligned bounding boxes. It also won’t allow you to rotate the zone.

2 Likes
1 Like

That is 90% overkill for this, 10% exactly what I provided.

1 Like

What does that mean?

But in any case, I’ve solved a problem

It means that it’s a fairly complicated way to get it done. EgoMoose’s library is great, but it does a lot of things that OP’s application really doesn’t need.

My code allows for a rotated part as well. It also doesn’t bother checking the Y axis, so as long as you’re somewhere above the part, it’ll detect you.

There’s also a lot of value in seeing a really simple implementation of a solution for a common problem in game development. Using a premade library that has a ton of functionality that you don’t understand fully doesn’t help you become a better programmer, it just helps you get your game out faster. But you shouldn’t underestimate the effects of becoming a better programmer, because it’ll pay off more in the long run.

2 Likes

You are right, and I think @SweetBloxxer714 is free to choose the method he wants to work with.

1 Like

I know it was annoying, and sorry! But i found a interessant tutorial on how to use Region3 for create a Zone Capture System, it was what you need (or not)!

Watch the whole video

3 Likes

Thank you for recommending me this. I know little about region 3 so this video will probably help me. Once I get my hands on a computer I’ll look into it. Ty

2 Likes

so, you could use @ForeverHD’s zone+ module

2 Likes

I watched the video and it’s exactly what i was looking for. However, I noticed that he used while loops to check if the character was in bounds. It’s preferable to stay away from while loops so i’ll just implement a onHeartbeat event that @mr_smellyman suggested. Ty

1 Like

Thank you for your response. I’ve read the module you suggested and it seems like that can work for my place too. I think i’ll just have to go with @mr_smellyman’s suggestion though

1 Like