Hi there,
Found something (though i don’t know if it was already discussed or if it’s already widely known):

This forces the type of all elements of PlayerGui to be the same as StarterGui (yeah , we don’t mention it a single time)
it’s really nice if you do your UI Scripts outside of StarterGui (I use ReplicatedStorage a lot)
This might also work on ServerScripts (if for some reasons you need to access PlayerGui from there)
Copy paste:
local Players = game:GetService("Players")
local player = Players.LocalPlayer :: Player
local playerGui = player:WaitForChild("PlayerGui") :: typeof(player.PlayerGui)
--StarterGui's service also works, but you already have player defined so it's not needed
Here you go, you now have a full Typechecked / Autocomplete support for your scripts !
8 Likes
i thought just doing player.PlayerGui already gives the same
also, is const already out???
Hello,
Firstly, yes const has been out for ~2 weeks.
Secondly:
This is done if you use WaitForChild() on PlayerGui, because PlayerGui is not instantly instanciated, so you have to wait for it to appear (considering you use PlayerGui at the start of a script that also starts when the client joins in).
WaitForChild() will not do the typecheck for you, thus why I made the post.
Also, yes, it did happen to me that PlayerGui wouldn’t be recognized if I used Player.PlayerGui. And finally, I script my UI inside ReplicatedStorage with RunContext on script set to Client, which is why I use PlayerGui.
I hope this is enough for you to understand !
but when accessing PlayerGui, the type check doesn’t put a question mark so that means it would never be nil tho
Player.PlayerGui exists, but it doesn’t exist right when the Player joins, it is made a bit after
So if your script runs before PlayerGui, it won’t work. Hence the use of WaitForChild. And there’s no typechecking on WaitForChild.
As for why does Player.PlayerGui is correctly Typechecked, it’s because PlayerGui is of the type of StarterGui, which contains all your UI
So if you use the typeof function with Player.PlayerGui, it knows what is inside. But WaitForChild doesn’t know what’s inside an instance.