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
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.
script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
if workspace.Nods:FindFirstDescendant(script.Parent.Parent) then
print("code from OG func")
end
print("parent changed")
end)
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)
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.