.ChildAdded & .DescendantAdded not running upon a child / descendant being added

Hey Devforum,
I’ve been pulling my hair out for the past few hours trying to figure out why this here block of code simply won’t run:

game.Players.LocalPlayer.Character.DescendantAdded:Connect(function(c)
	print("hi")
	
	if c:IsA("ForceField") then
		-- don't need to worry about this code here (it's irrelevant)
	end
end)

When replacing the comment with the code that’s actually there, this doesn’t work.

Essentially, what it’s doing is looking to add an effect to a player’s screen when they get a forcefield and removing aforementioned effect when the forcefield is destroyed (not seen in code sample).

In an attempt to track down the problem, I had placed prints all over the script after everything to find exactly where it went wrong (no errors were being printed).

I ran it, and not even the first “hi” was printed.

I’ve been searching through Devforum, ScriptingHelpers, Youtube, and none of their circumstances apply to me, and none of their solutions work for me.

Some information that might be useful:

  • This code is handled in a Localscript.
  • Aforementioned Localscript is in a GuiObject located in StarterGui
  • The GuiObject has ResetOnSpawn enabled.

Any information, solutions, or other forms of help are much appreciated.

1 Like

These issues can cause your script to fail at some point:

  • The player may not have a character when the script runs.
  • The script only connects to the first character, not new ones.
  • This is client sided, players only see their own forcefield changed/removed.

This is the output from your code, when placed in a localscript in starterGui. If it isn’t firing at all, it’s probably not being connected.

  04:30:46.732   ▶ hi (x267)  -  Client - LocalScript:7

We’ll need to know what you do before this function

Hi Red,

Thanks for the detailed writeup! I’ve taken a few minutes to replicate a very basic replica of your setup, and your code copied into it produced the intended output. See attached screenshots:
image

image

image

Since it appears that this code works appropriately in a sandboxed environment, it may be best to show some more of the scope surrounding your code. For example, is it possible that this connection isn’t being properly established due to an error upstream in the code? Does the GuiObject still exist when you’re adding the part? Some of these more circumstantial pieces may be causing your symptoms, but I don’t believe that it is the code you listed specifically.

Best,
Speedy

Once again, I have forgotten about a very simple thing which caused the script to not function.

I had neglected to consider that there was a while loop above the code, one that runs forever. I placed the function below the while loop, which caused the function to never be connected.

oops

Thanks everyone for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.