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.
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?
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.
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.
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.
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)!
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
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
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