Unexpected script rerunning when changing tool parenting?

I am making a custom backpack/inventory for players. I have a folder stored inside of the player itself (not the character, but in Players). Inside of the folder I put tool instances. When the player clicks a key, the tool is reparented from the folder to their character, thus equipping the tool. When they equip another tool, the old tool is reparented to their folder in their player. The unexpected behavior is this: every time the tool is reparented to the player, its script is run. Even a simple script with print(“Hello World!”) will print every time the tool is reparented to the character.

If anybody knows, what are the circumstances for this? Is it just that any tool being parented to a character has its script rerun every time? Or does a script always rerun when its ancestor is being changed? I haven’t noticed this happening in other situations.

I think the problem is that the LocalScript won’t run inside of the player instance causing it to start run again when it’s parented to the character again. One possible solution would be to store the tools in the PlayerGui (I don’t think it’s a good practice) or in the Backpack (You would probably have to turn off the default Backpack gui. You could use this to achieve that.).

1 Like

Thanks for your response. I’m using server scripts, but I think you are right either way. Your response reminded me that it would only run the functions after the reparenting was complete. This makes sense.