-- CHARACTER HANDLER
-- This enables character data (volume, HumanoidRootParts, etc) to be handled on
-- an event-basis, instead of being retrieved every interval

local function updateCharactersTotalVolume()
	charactersTotalVolume = 0
	bodyParts = {}
	-- We ignore these due to their insignificance (e.g. we ignore the lower and
	-- upper torso because the HumanoidRootPart also covers these areas)
	-- This ultimately reduces the burden on the player region checks
	local bodyPartsToIgnore = {
		UpperTorso = true,
		LowerTorso = true,
		Torso = true,
		LeftHand = true,
		RightHand = true,
		LeftFoot = true,
		RightFoot = true,
	}
	for _, plr in pairs(players:GetPlayers()) do
		local charRegion = ZoneController.getCharacterRegion(plr)
		if charRegion then
			local rSize = charRegion.Size
			local charVolume = rSize.X*rSize.Y*rSize.Z
			charactersTotalVolume += charVolume
			for _, part in pairs(plr.Character:GetChildren()) do
				if part:IsA("BasePart") and not bodyPartsToIgnore[part.Name] then
					table.insert(bodyParts, part)
				end
			end
		end
	end
end
players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid", 3)
		if humanoid then
			updateCharactersTotalVolume()
			for _, valueInstance in pairs(humanoid:GetChildren()) do
				if valueInstance:IsA("NumberValue") then
					valueInstance.Changed:Connect(function()
						updateCharactersTotalVolume()
					end)
				end
			end
		end
	end)
end)
players.PlayerRemoving:Connect(function(plr)
	updateCharactersTotalVolume()
	playerExitDetections[plr] = nil
end)


