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.
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:
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.
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.