Even if signals are Deferred, I expected them to still fire so I can run code when the player drops an item on the ground.
This code could clean up currently playing animations, or hide GUIs.
Sorry to bump this. I was wondering if there was an update on this? I understand that this is due to the LocalScript execution halting (Because the tool was dropped) and therefore disconnecting the listeners associated.
Is there a better way to approach this? Unfortunately this results in an issue where associated GUI elements are not cleaned up.
Bumping this with a simple workaround in case anyone needs it, since this bug has been present for almost a year now:
Move all of your LocalScript code into a ModuleScript in the same tool.
Add a new LocalScript to the tool with the following code:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = script.Parent
require(tool:WaitForChild("ModuleScript")) -- Replace with the name of the ModuleScript if you changed the name
-- Parent self to PlayerScripts so the script keeps running for a moment after the tool is dropped
script.Parent = player.PlayerScripts
tool:GetPropertyChangedSignal("Parent"):Connect(function()
if tool.Parent ~= player.Backpack and player.Character ~= nil and tool.Parent ~= player.Character then
-- Tool is no longer in the backpack and not equipped, so it was either dropped or destroyed
-- Put self back in the tool so this script stops running
script.Parent = tool
end
end)
tool.Destroying:Once(function()
script:Destroy()
end)
You can also avoid putting the LocalScript in the tool altogether and use an OOP object for your tool’s client code instead. Here’s more information on the topic if you’re unfamiliar: