The DescendantAdded function doesn't run (it doesn't print anything)

I tried moving the Nod Health Bar function to another script, still doesnt print anything

Yes, the nods do get added to the folder, but it doesnt get detected for some reason
image

how would I detect the change of the parent btw? is there a function for what?

the script cant be disabled cuz it does add the nods to the folder (its the same script that handles the nod health bar and the nod generation)

You could use
obj:GetPropertyChangedSignal("Parent"):Connect(function() end)

Yes

local currentparent = script.Parent.Parent
script.Parent.Changed:Connect(function()
if script.Parent.Parent ~= currentparent then
print("parent changed!!")
end
end)

For this it’s better to use GetPropertyChangedSignal instead of .Changed since we are specifically looking for when the parent changes and not other stuff. .Changed will make unnecessary calls Everytime something changes

1 Like

I’ve never heard of that function! I’ll give it a try for sure

1 Like

Copy your scripts to a new game and see if it works there.

Here’s the documentation:
https://create.roblox.com/docs/reference/engine/classes/Instance#GetPropertyChangedSignal

1 Like

image

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function() 
	print("parent changed")
end)

image

1 Like

Ok so it detected the change in parent, you should probably check if the parent is a descendant of the Nods folder and then run your code as you would have in the DescendantAdded

Are you connecting the event before adding your instance? The only thing I can think of is that the event is being connected after the instance is added, causing the new descendant to not be detected.

Something like this should do:

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function() 
    if workspace.Nods:FindFirstDescendant(script.Parent.Parent) then
        print("code from OG func")
    end
	print("parent changed")
end)

image

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function() 
	print("parent changed")
	if script.Parent:IsDescendantOf(workspace.Nods) then
		print("the nod is a descendant of workspace.nods")
	end
end)

Ok so replace the print with the code from before:

local nod = script.Parent
nod:GetPropertyChangedSignal("Parent"):Connect(function() 
	print("parent changed")
	if nod:IsDescendantOf(workspace.Nods) then
		print("the nod is a descendant of workspace.nods")
        local Nod_Durability = nod:GetAttribute("Durability")
        local HealthBar = nod:FindFirstChild("HealthBar"):FindFirstChild("Background"):FindFirstChild("Durability")
	end
end)

no, the instance gets added BEFORE it runs the event (cuz its in the same script overall, but the function that adds the instance to workspace.nods is earlier in the script)

Then that’s your problem, connect the event first, then add your instance, alternatively you can loop through the children and manually call your DescendantAdded function.

local function DescendantAdded(nod)
    print(nod)
    --do stuff
end

for _, descendant in pairs(folder:GetDescendants()) do
    DescendantAdded(descendant)
end

folder.DescendantAdded:Connect(DescendantAdded)

yea I just moved the DescendantAdded event BEFORE the nods generation and it fixed the issue

3 Likes

So was the rest of a script in a loop or in a different connection? It seems as if it were a loop since the connection happened later

Since scripts run synchronously it doesn’t matter if you add the instance a single line before, it just won’t be detected.

Here’s a little fun example code:

local newFolder = Instance.new("Folder")
newFolder.Name = "Folder that is never detected on DescendantAdded"
newFolder.Parent = script

script.DescendantAdded:Connect(function(newDescendant)
	print(newDescendant)
end)

local newFolder = Instance.new("Folder")
newFolder.Name = "Folder that is detected on DescendantAdded"
newFolder.Parent = script

I am completely aware of this, that’s why I asked if it was in a connection, because if it was in a different connection then it wouldn’t have mattered if something else triggers it to happen, but with a loop or just adding it normally then that’s a different story.

1 Like