I have two tools in ServerStorage. These two tools have an identical LocalScript inside both of them. With a server script, I am distributing these two tools to the player. One of the tools I am equipping for them, instantly after being parented to their Backpack. The tool that gets equipped runs its local script twice, once when it is created and parented to Backpack, the next time when the tool is equipped. I have verified this using prints in the local scripts. This happens 100% of the time. The local script in the tool should only run once, on creation.
Reproduction steps:
Create an empty place
Set SignalBehavior property of Workspace to Default
Create two tools, both with an identical LocalScript with a print inside it to verify that it is running. Put these tools in ServerStorage.
Create a script in ServerScriptService that, within a CharacterAdded event, clones these two tools and parents them to the player’s Backpack. Then, equip one of the tools with humanoid:EquipTool() OR by manually parenting it to their character.
Run a studio test and observe that the output displays 3 prints, rather than the expected 2 prints, as there are only 2 tools being added to your Backpack.
This is what the output looks like if the bug occurs. I am printing the tool and tool.Parent. The tool that was equipped, Tool2, prints twice, and by the second time it runs it is parented to the character (i.e. when it is equipped).
There are multiple ways that stop the bug from happening which causes the expected behaviour (the localscript inside runs only once):
Include a task.wait() either before creating the tools, or in between creating and equipping.
Setting the SignalBehavior property of Workspace from Default to Deferred.
Simply do not equip the tool for the player (letting them equip it manually).
Thanks for reporting this! As strange as it sounds, this is technically correct behavior, and it would be difficult to fix, so we’ve made the decision not to address it. Relevant docs (emphasis mine): CharacterAdded event:
This event fires soon after setting Player.Character to a non-nil value or calling Player:LoadCharacter(), which is before the character is parented to the Workspace.
The Backpack can also store Scripts and LocalScripts, which run when placed in a player’s Backpack.
Basically, in the place you sent with Immediate signal behavior, the CharacterAdded event fires before the Character is in the Workspace. But, the LocalScript gets placed inside that Character’s Backpack, which is a valid place to run scripts, so it runs, and then is parented to the Character. Then, after the event code finishes, the Character is added to the Workspace. For the LocalScript, this is a different Workspace than it was in previously, so it runs again.
Adding task.wait() anywhere before the call to EquipTool() allows the Character to be added to the Workspace before the LocalScript is equipped, so it doesn’t appear to change Workspaces (which would cause it to restart). Very strange, but again, technically correct! On the other hand, using Deferred SignalBehavior fixes this issue entirely, since CharacterAdded only fires after the Character is added to the Workspace.
TL;DR: We know this behavior may be somewhat unintuitive, and we appreciate your understanding. Use OP’s workarounds if this behavior is giving you trouble in your experience. If you have any other concerns or questions, feel free to let us know!