So i found what the problem is. Afx thinks it’s an AVLayer instead of CompItem when i use this line :
var selection = mainComp.selectedLayers[0];
I edited the script like this and it works :
var mainComp = app.project.item(1);
var selection = mainComp.selectedLayers[0].source;
alert(selectedComp.numLayers);
The result is exact number with this way. I’m not sure if it’s the correct way to do this but it works. If there’s an alternative way to do this, i would like to know. I hope it helps 🙂