Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I find a players Gui through a script?

function fun(player)
local label = player.PlayerGui.TimeLeft.Frame.TimeLeftLabel
local ready = player.PlayerGui.TimeLeft.Frame.Ready.ReadyToVisit
end
game.Players.PlayerAdded:connect(fun)

Error output comes out with "PlayerGui is not a valid member of Player "WHAT AM I DOING WRONG???

Here is the Workspace thing

Players
	Player1
		PlayerGui
			TimeLeft
				Frame
					Ready
						ReadyToVisit
					TimeLeftLabel

3 answers

✓ Accepted answer

The issue is that it hasn't loaded yet. Use :WaitForChild() to pause the script until the item is found.

Here:

function fun(player)
	player:WaitForChild('PlayerGui')
	wait(0.5) -- let the other stuff load up
	local label = player.PlayerGui.TimeLeft.Frame.TimeLeftLabel
	local ready = player.PlayerGui.TimeLeft.Frame.Ready.ReadyToVisit
end

game.Players.PlayerAdded:connect(fun)

in a local script you can do something like this in a server-sided script i suggest using the PlayerAdded event

Player = game.Players.LocalPlayer
repeat wait() until Player:FindFirstChild("PlayerGui")
local PGui = Player:FindFirstChild("PlayerGui")
local plr = game.Players.LocalPlayer
repeat wait() until plr:FindFirstChild("PlayerGui")

function findInPlayerGui(name, parent)
	if plr.PlayerGui:FindFirstChild(tostring(parent)) ~= nil then
		prnt = plr.PlayerGui:FindFirstChild(tostring(parent))
		if prnt:FindFirstChild(tostring(name)) ~= nil then
			return prnt:FindFirstChild(tostring(name))
		else
			return false
		end
	else
		return false
	end
end

function fun(player)
local TMFrame = findInPlayerGui("Frame", "TimeLeft")
local label = findInPlayerGui("TimeLeftLabel", TMFrame)
local TMReady = findInPlayerGui("Ready", TMFrame)
local ready = findInPlayerGui("ReadyToVisit", TMReady)
end

fun(plr)

This is in local script, also this might be long but its good.

This discussion is preserved as it was. New answers, comments, and votes are closed.