Plugging in here: if you’re using this from a LocalScript, it will not run as expected. LocalScripts run at a time when the player has already joined, so a LocalScript will not see when PlayerAdded is fired for the current client. Its use is unnecessary.
You can use the LocalPlayer variable as an alternative if you need to get the player. It is implicitly defined for LocalScripts.
local plr = game:GetService("Players").LocalPlayer
plr.PlayerGui -- so on