Unable to count number of team members in region

Hello! I’m creating a payload game similar to TF2.

However, even though it does count the number of players in the region, it doesn’t work if I use an if statement to check for the player’s team. I’m genuinely stumped. Help please!

--Objects
local Cap = script.Parent.CaptureArea
local Model = script.Parent.InvisibleWall

--Services
local players = game:GetService("Players")
local Team = game:GetService("Teams")


function DefendingTeam()
	
	local min = Cap.Position - (0.5 * Cap.Size)
	local max = Cap.Position + (0.5 * Cap.Size)

	local region = Region3.new(min,max)
	
	local t = {}
	
	local parts = workspace:FindPartsInRegion3(region)
	for _, v in pairs(parts) do
		local plr = players:GetPlayerFromCharacter(v.Parent)
		if plr and not table.find(t, plr) then
			if plr.Team.Name == "Defenders" then
				table.insert(t, plr)
			end
		end
	end

	return t
end

function AttackingTeam()

	local min = Cap.Position - (0.5 * Cap.Size)
	local max = Cap.Position + (0.5 * Cap.Size)

	local region = Region3.new(min,max)

	local t = {}

	local parts = workspace:FindPartsInRegion3(region)
	for _, v in pairs(parts) do
		local plr = players:GetPlayerFromCharacter(v.Parent)
		if plr and not table.find(t, plr) then
			--print("This works")
			--print(plr)
			if plr.Team.Name == "Defenders" then
				table.insert(t, plr)
			end
		end
	end

	return t
end

while wait(0.1) do
	DefendingTeam()
	AttackingTeam()
	print("Defenders: ", #DefendingTeam())
end

I figured it out… The functions didn’t have “local” in front of them, dunno how that happened -_-

FYI, Region3-based spatial-querying has been deprecated for three years now. There is a new spatial query API for you to use

Yeah, I already switched to using “GetPartsInPart” since it was just a lot more consistent than region3

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.