Interesting LocalScript

Hecc.rbxl (28.7 KB)
Basically I created a gui object in StarterGui, inside I created a LocalScript with these lines of code in it:

print(script:GetFullName())

script.Parent.AncestryChanged:Connect(function(child, parent)
	if parent.Name == "PlayerGui" then
		print("hecc")
	end
end)

The LocalScript is enabled, everything is untouched I just added the lines of script in it.
As you can see on the first line I print the full ancestry for the script, because I am aware that this LocalScript will run once it is parented to the PlayerGui.

On the other lines you can see that I simply connect an event to fire once the Ancestry of the script is changed, which apparently fires even though the script precisely prints on the first line that it is already in the PlayerGui.
So in conclusion the script will end up printing both the fully ancestry of the script, saying that the script belongs to the PlayerGui and the second print (‘hecc’), stating that the event was fired and the ancestry was changed to the PlayerGui, even though on the first line it already said it belongs to the PlayerGui.
I am really curious if anybody has an idea on this, I will attach a place where you could testrun and see this in action.

Ok so,

You are basically wondering why it prints it out correct because there was no Ancestry Change correct?

If so I think the reason why is because the UI does not fully spawn in? I am just thinking that could be it cuz when I put a wait 10 seconds in the script it does not print the “hecc” showing there was no Ancestry Change.

Although I don’t understand why it would print the full name which shows it is already in the player gui if it had not fully spawned in.

Script:

print(script:GetFullName())

wait(10)

script.Parent.AncestryChanged:Connect(function(child, parent)
	if parent.Name == "PlayerGui" then
		print("hecc")
	end
end)
1 Like

Yeah, you’re right in a way, it seems the timing is almost perfect for it to detect AncestryChanged.

Maybe this is because the print(script:GetFullName()) and AncestryChanged event happened at the exact same time. In fact, when I added wait() between the print statement and the connect function, “hecc” didn’t appear, that’s because the wait() made the AncestryChanged event delayed by a small period, so it didn’t fire.

Also, I tried this code:

print(script:GetFullName())

print(os.clock())

script.Parent.AncestryChanged:Connect(function(child, parent)
	print(os.clock())
	if parent.Name == "PlayerGui" then
		print("hecc")
	end
	print(script:GetFullName())
end)

and the difference in time was 0.64 milliseconds which is very very small. For comparison, a frame is 17 milliseconds.

1 Like

Yeah, time is definitely the issue. Gotcha and thanks!

2 Likes

Wouldn’t Parent have to be capitalized? Correct me if I misunderstood.

The AncestryChanged event includes two parameters, child and parent . child refers to the Instance whose Instance.Parent was actually changed. parent refers to this Instance ’s new Instance.Parent (You can learn more here AncestryChanged event).

He chose the names child and parent as a convention, he could’ve wrote this:

print(script:GetFullName())

script.Parent.AncestryChanged:Connect(function(foo, bar)
	if bar.Name == "PlayerGui" then
		print("hecc")
	end
end)
2 Likes