I searched a lot for how to do this
I tried all the solutions I read and none of them worked
I couldn’t make the username unique
All of these methods work correctly when I create a new user while I am still logged in with the current user, but when I log out of the user and go to create a new user, they become inactive and a the new user is created with a duplicate username.
Can’t you just set the Current User’s slug to user’s input:lowercase? That will always be unique and if there’s a user with the same name it won’t work.
The second thread has both of the answers you need (use slugs or do a count of a search), and if you can’t get the latter to work, it is likely because the default privacy rule on the User data type is getting in the way of the search.