Simple AppleScript script terminates early
I can't test this properly without messing up my own music library, but I suspect the problem is with this line:
set selectedTracks to selection

If Music is being true to form, this would pass back the current selection as a reference to a list of tracks. Passing by reference means that the list is stored in Music.app and the script retrieves items as needed; it also means that if the selection changes during script execution (which it probably does, since you're changing track names) it will throw off the indexes and references within the script. This might cause errors, or may just trick the repeat loop into think it has worked through all the items, when in fact they've merely disappeared from view.
The solution would be to dereference:
set selectedTracks to (get selection)

This should convince Music.app to pass an explicit list at the beginning of the script, so that changes in the selection would not propagate back to the script.