How to make script run when it's parent is changed

Made this script recently and it does not work when its parent is changed. Is it a script that should work or did I write some weird script that wouldn’t work at all. It is a bit confusing since the script is trying to detect its own parent.

script.AncestryChanged:Connect(function(_, parent)
	if parent.Name == "BulletTrail" then
		local TS = game:GetService("TweenService")
		local p = script.Parent
		local Info = TweenInfo.new(1)
		local Tween = TS:Create(p,Info,{Transparency=1})
		
		Tween:Play()
			Tween.Completed:Connect(function(playbackState)
			print("Done")
			p:Destroy()
			script:Destroy()
			end)
		
	end
end)

Basically, I just want a script that can detect when its parent changes and if the parent is named “Bullettrail” it will run the following code.

3 Likes

Try:

script:GetPropertyChangedSignal("Parent"):Connect(function()
	if script.Parent and script.Parent.Name == "BulletTrail" then
		local TS = game:GetService("TweenService")
		local p = script.Parent
		local Info = TweenInfo.new(1)
		local Tween = TS:Create(p,Info,{Transparency=1})
		
		Tween:Play()
			Tween.Completed:Connect(function(playbackState)
			print("Done")
			p:Destroy()
			script:Destroy()
			end)
		
	end
end)

If this doesnt work I’d recommend just simply making the script disabled, then enabling it manually as I’m presuming you’ll be cloning the script into a BulletTrail anyway

5 Likes

Thanks for the help! (letter limit)

2 Likes