Is it possible that things are being added/removed in such an order that the name of your player node isn't always "Player"? IE, if you try to add a new instance of player before the existing one is freed, you might end up with a node called "Player 1" instead of "Player". I'd recommend running the scene from the editor and taking a look at the Scene Tree (in the Output/Debugger area) to see if this is what's happening.
If that is the case, one way you can avoid this is to change how you verify that a node is the player. Instead of this:
if body.get_name == 'Player':
you can reference the actual script that goes with the player (assuming it's not a built-in one; if it is, this might be tricky, so I'd recommend saving it as a unique script file first.)
Anyway, if your player script is player.gd
, you could replace the line above with this:
if body extends 'res://path/to/player.gd':
(substitute the actual path to your script, of course)
Or, if that doesn't work (I'm not sure how well extends
plays with the actual script on a node; usually I would define a script like player.gd and then have a script on the player node that just extends that), try this:
if body.get_script().get_path().find('player.gd'):
In either case, it doesn't actually matter what the node is called. If it behaves like a Player, it'll get handled as a Player.