Gui is not a valid member of PlayerGui

This script is meant to detect when a player joins the game, fade from black (the i loop) and then play a sound. However, when I launch the game, nothing happens with the GUI (FadeGui below) and “FadeGui is not a valid member of PlayerGui “Players.(the player).PlayerGui”” is returned.
I’ve checked and the GUI shows in the Explorer under PlayerGui, so I’m slightly bamboozled.
Any help on this would be appreciated as I am in no way a seasoned user of Lua.

(This is a standard Script, not a LocalScript, running in ServerScriptService)

local Players = game:GetService("Players")
local Sound = game:GetService("SoundService")
_G.globalPlayerId = 0

Players.PlayerAdded:Connect(function(player)
	
	player:WaitForChild("PlayerGui")

	local playerid = player.UserId
	_G.globalPlayerId = playerid
	
	player.PlayerGui.FadeGui.Frame.BackgroundTransparency = 0

	print("Player ", playerid, " joined")
	
	wait(1)
	
	for i = 0, 1, 0.01 do
		player.PlayerGui.FadeGui.Frame.BackgroundTransparency = i
	end
	
	Sound.MenuMusic:Play()

end
)


Thanks!

1 Like

I am a bit confused with the

you should do this:

local plrGui = player:WaitForChild("PlayerGui")
local frame = plrGui:WaitForChild("FadeGui").Frame
--- do what you want with the frame instance like backgroundTrans

The fix for your issue is simple. Descendants of PlayerGui take a while to load after the player is added and are not loaded immediately after the player, or the playerGui is. This means that when :PlayerAdded(p: Player) fires, neither playergui or its descendants exist yet.

You have partially addressed this issue by waiting for the playergui when the event fires
by doing

player:WaitForChild("PlayerGui")

However, the descendants of playerGui are not loaded in the moment it is. This means that this line needs to be replaced with

player:WaitForChild("PlayerGui"):WaitForChild("FadeGui")

Also, I would advice replacing

for i = 0, 1, 0.01 do
	player.PlayerGui.FadeGui.Frame.BackgroundTransparency = i
end

with

game:GetService("TweenService"):Create(player.PlayerGui.FadeGui, TweenInfo.new(1), {BackgroundTransparency = 1}):Play()

since the loop you made will run pretty much instantly and not make a transition, while TweenService’s Tweens will create a smooth transition and you can set it to have a transition style like sine or quartic.

If it were for me I would do this all in a local script under StarterPlayerScripts or under the gui. In your case it looks like the music may only play for one player at a time or reset with every new player, as the music sound is one object and you restart its playback every time a player joins, which is not a good practice even if this is a singleplayer game.

1 Like

Thanks for that! I suspected that it was something to do with the GUI elements not loading in yet, and as you said the WaitForChild was an attempt at remedying that.

I didn’t notice when posting that I had made a mistake in the fade-in loop, omitting a wait command and therefore rendering the loop useless. This was just an oversight on my part but I’ll try your TweenService suggestion as it seems a much more versatile method.

This is a singleplayer game, which is why I wasn’t too worried about the sound being controlled server-side. I do agree that I could organise my scripts better, however, and I’m planning to tidy things up before a release.

Thanks for your in-depth answer and explanation!