Count the number of custom module instances of a page


Is there a way to dynamically detect the number of instances of a custom module on a page?
Or is there a way to detect the first instance of a module

I’d like to be able to run a for loop based on that information to modify the code output.