On Script Parent's Parent Set To Nil

I want to :Connect a function to when a script.Parent’s parent get’s set to nil. (When script.Parent.Parent becomes nil.)

I’ve tried using GetPropertyChangedSignal, tried using AncestryChanged, and a couple others I don’t remember about. They only worked when the parent wasn’t set to nil. (As in when I equipped or packed away the tool the script is inside). If the parent was set to nil, it didn’t fire, I put a print() function inside the function and it only fired when the parent wasn’t set to nil.

Didn’t look on the forum for solutions because none of the suggested posts were relevant.
I’ve been looking this up on the internet for about 3 days now

-- tried using tool.AncestryChanged as well, same problem
--tool:GetPropertyChangedSignal("Parent"):Connect(function()
-- NOT EVEN THIS RUNS! If parent is set to nil, NOTHING PRINTS AT ALL
--	print(tool.Parent)
--	if tool.Parent == nil then
--		print("is nil")
--		ondes:FireAllClients(tool) end
--end)

I also tried firing the client when the item gets parented to nil.

	if #picked == rcplen then
		local slist = {}
		for i=1,#picked do
			picked[i].Parent = nil
			ondes:FireAllClients(picked[i])
			local cs = picked[i]:FindFirstChild("Craft Sound")

Edit: Here’s an image:

Once the parent gets set to nil (ie, the tool gets destroyed) the script stops running. This means no events listeners will fire. You can check this by running a while loop that prints something, and you’ll notice that it stops printing when the tool’s parent is set to nil. If you need something to run when the tool is destroyed you could use a bindableFunction that you Invoke before you destroy the tool.

2 Likes

The problem is I’m trying to get this into a localscript from a serverscript.
I’m already using a Remote Event to do this, but my localscript is inside the tool. Is there any workaround?
Can I parent the LS to the player and put it back when the tool gets unequipped?
I didn’t know scripts didn’t run once parented to nil, so that’s a big step in the problem

What are you trying to get into a localscript? More details on what you’re trying to achieve would be helpful.
You can clone the localscript to the player, however a more efficient route would be using ChildAdded and ChildRemoved on the localplayer’s backpack to detect when a tool gets added or removed.

My localscript is inside a tool, and the tool is a gun with an aim animation. When you equip the gun, it makes your screen slightly red and slows your cursor when you aim. If you craft a new item that requires the gun, and you do it while holding down the aim button, it never runs the unequip function. Honestly, I feel like this is a bug on roblox’s end, since an item being destroyed makes it technically unequip the tool. When you unequip the tool normally, it runs a function to undo all the changes it made, like the sensitivity and the red screen overlay, and it also stops the animation. Doing that crafting glitch doesn’t let the script run that “undo” function, so your arm is stuck in the aiming position, and your sens / screen redness stays the same.

Would this work? Just add a localscript that checks for those things and if the removed child’s name is whatever the tool’s name it resets anything the other script changed.

1 Like

Maybe the issue is that you’re Destroying the tool when perhaps a normal unequip would be more appropriate, followed by removing it from the backpack or calling Destroy() once it’s in the backpack and the unequip event has been handled.

What’s the use case for Destroying whilst equipped?

Honestly, I didn’t think of that, my crafting GUI takes multiple items at once, and I didn’t really expect that to be a problem. Thanks.

I’ll try this too, thanks for the help

Okay, so I added the localscript with the ChildRemoved script, but the problem is that I can’t stop the other animation because it’s in a different script. How can I stop that one animation from the other script?
Edit: It’s a Server Script, not a Local Script. I can make another Local Script, though.
Edit 2: Nevermind, I made a localscript, I forgot you can’t do this stuff in a serverscript. The problem is still there, though.

Okay, I managed to finish this using the ChildRemoved event plus these 2 functions:

What I did was a really hacky way, but I’ll try to polish it up later. Basically, I looped through the playing animation tracks, and took their Animation value. Then, if that animation’s Name was the same as “Aim Anim” (The name I used for all my aim animations) then I just :Stop() it.