I have a tool which when equipped it will have a GUI pop up and once used, it will destroy the tool.
The issue that I have is that once I destroy the Tool from the server script, the client script that I have under the tool doesn’t have the Tool.Destroying event fire, which means the GUI will stay there forever. The weird thing is, I use this tool in one of my other games, and it prints just fine, but with using the tool on a plain baseplate, it does not fire (which might be because there is more stuff happening on the server on the other game, which leads to a delay in the tool actually being destroyed, allowing the event to be fired or something??)
Snippet of the local script code:
function onDestroying()
print("destroying!!!")
if currentFrisbeeChargeGUI ~= nil then
currentFrisbeeChargeGUI:Destroy()
end
end
Tool.Activated:Connect(onActivated)
Tool.Equipped:Connect(onEquipped)
Tool.Unequipped:Connect(onUnequipped)
Tool.Destroying:Connect(onDestroying)
I’ve done some research, but I cannot find a good fix for this, is there any other way to detect when the tool is being destroyed so that I can have the GUI be destroyed and not be there forever?
Not sure if this is helpful but I think Destroying event only fires when called directly on the instance. Try AncestryChanged:Connect(function( child,parent) … ) and check if the parent is nil
Just unequip the tool before destroying and make the ui go away on the unequip event instead
I’m assuming it doesnt work because the local script is IN the tool being destroyed… so its destroyed too before the event is fired
Yo, i have found an interesting way to fix this issue.
Simply use the AncestryChanged event and check if the “game” object is an ancestor of your object.
local AncestryConnection: RBXScriptConnection
AncestryConnection = Object.AncestryChanged:Connect(function()
if Object:FindFirstAncestorOfClass("DataModel") then return end --DataModel is the class of "game" object
AncestryConnection:Disconnect() --The event can fire multiple times before ceasing to exist
--Do your stuff
end)
I believe the issue is that the script gets destroyed before the event runs, perhaps parenting it to something else through code could be used as a workaround?
But remember to call script:Destroy() in the Destroying event to avoid other issues
I haven’t really tried this as Humanoid.Died works for my use case
I’m not sure if this is still a problem for anyone, but if it is, I usually get around this by using a bindable event to pass the function I want to run and the instance I want to listen for into another script. Then, I hook up that function to the .Destroying event of the instance inside that other script. This way, that code can run after the original script gets destroyed.