How would you solve the problem then? We can’t have masses of children walking around unsupervised because it is unsafe, we can’t afford to employ more staff to be on duty so what is the answer?
You limit the toilets that can be used and lock the others. Depending on the size and layout of the school you have one toilet that can be used mid lesson, next the HT office or an admin office.
You could make the toilets to be used the accessible ones so the student has to get a key (from the teacher or the office) and the door automatically locks when they leave.
I've done lots of supply, some schools have doors that can only be opened with an ID card, if you have that system then you can automatically lock toilet doors so people can get out but not in unless they have 'permission' on their ID card.
Most schools have SIMS or some thing similar, so you can track who is going to the toilet every lesson and investigate why.