Inconsistencies with GetPropertyChangedSignal

My issue is with using GetPropertyChangeSignal(“Parent”) to wait for the player’s parent to be changed. (when the player is removed)

Issue:
I have one script which uses a PlayerAdded event and waits for the player’s parent to be changed. (so it can disconnect a .Chatted event)
I also have another script which uses a PlayerAdded event and waits for the player’s parent to be changed and then will disconnect a .Chatted Event

How I wait for the parent to be changed:
player:GetPropertyChangedSignal(“Parent”):Wait()

However, with the first script, it will automatically resume despite the parent not being changed. This doesn’t happen in the second script.
I’ve made a workaround, but i’m still curious why it resumes even though the parent wasn’t changed, and also why it doesn’t also happen in the second script even though I use the same mechanism.

I don’t have answers to your questions however I can suggest using AncestryChanged to detect when a Parent changes.

What about the PlayerRemoving event?

Of course. I was referring to any instance. Not just Player instances.

Then I would have to make a table, store the connections in the table, and then when the player leaves, disconnect all the connections, along with removing the connection from the table. Simply waiting for the parent to be changed is simpler. Also, i’m probably going to stick with my work around, considering it factors in if they player has already left. (it yields before the connection is established, so using player removing might make it so some of the events aren’t disconnected, although only if they leave almost immediately)

My work around:
wait()
local h = not player.Parent or player:GetPropertyChangedSignal(“Parent”):Wait()

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print("Current parent:", player.Parent)
	player:GetPropertyChangedSignal("Parent"):Wait()
	print("Next parent:", player.Parent)
	player:GetPropertyChangedSignal("Parent"):Wait()
	print("Final parent:", player.Parent)
end)

-- Current parent: Players
-- Next parent: Players (fired immediately after)
-- Final parent: nil (left the game)

Same output with AncestryChanged:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print("Current parent:", player.Parent)
	player.AncestryChanged:Connect(function(_, newParent)
		print("Next parent:", newParent)
	end)
end)

-- Current parent: Players
-- Next parent: Players
-- Next parent: nil

This behavior is awkward, but you should use PlayerRemoving anyways.

1 Like

Would using player removing make a difference in terms of performance?
Also, how come it wasn’t happening to the second script?

This shouldn’t impact performance in any meaningful way. You would have to post the code to get a good guess.