There are many reasons for people to go to rooftops.
One of those reasons would be Combat.
if the characters spend all their time in a crowded school or office
building, the roof might be the only place that they feel like they have any room to maneuver for a fight. Bonus points if the building has some other reason to be up there, such as a rooftop garden, heliport, or basketball court. source
Usually the protagonist doesn't want to fight in a crowded place where people might get hurt ( there are some exceptions ). They usually move to a place where the least damage can be done to the surroundings Aka the rooftops.
Another reason would be alone time
With the usual size of homes in Japan, sometimes the only option available to a character who desperately needs to be alone is to climb up and sit on the roof of their house. There they can look up at the night sky and contemplate their place in the universe in peace and quiet. source
In allot of story's the main protagonist lives in a crowded city and as described the only place to go would be The roof top
As for schools in japan there are many different layouts. Smaller schools with less place for club activity's might actually place clubs as the gardening club on the roof. Allowing the club to exist without taking to much space away for other clubs making the roof a more pleasant place to stay for students there for preferring them to eat lunch and hangout on the roof more often.
In other scenarios the school might not use the roof for anything and there will be no attention on the roof. There for making it the perfect place for socially awkward and or weird protagonists to eat there lunch without getting disturbed.
I left my answer to these 2 specific as your pictures hint the most to these 2 scenarios. if you where to list all potential roof top scenarios this answer would become to broad/big