PlayerGui is not a valid member of Player (LocalScript)

Hello,
I’m working on a game with my friend. I scripted that if a player clicks on you, the other player will get a notification UI with 2 buttons. In my code, the PlayerGui will not get found. Here’s my script:

	local player = frame:FindFirstChild("Name")
	-- game.ReplicatedStorage.Remotes.SendFriendRequestServer:FireServer(playerToAdd)
	
	local playerWhoAdded = game.Players.LocalPlayer
	local p = game.Players:FindFirstChild(tostring(player.Text))

	for _, pl in pairs(game.Players:GetChildren()) do
		if pl.Name == player.Text then
			local UI = game:GetService("Players"):FindFirstChild(player.Text).PlayerGui:FindFirstChild("RequestPopUp")
			local BG = UI:FindFirstChild("Background")
			local Template = BG:FindFirstChild("Template")

			local cloned = Template:Clone()
			cloned:FindFirstChild("PlayerWhoAdded").Text = playerWhoAdded.Name .. " |"
			cloned:FindFirstChild("PlayerWhoAddedValue").Text = "Value: " .. tostring(playerWhoAdded:FindFirstChild("PlrFolder"):FindFirstChild("PlrValue"):FindFirstChild("PlayerValue").Value)
			cloned.Visible = true
		end
	end

The other code isn’t important. Error: PlayerGui is not a valid member of Player “…”. It’s a localscript located in StarterGui.
Thanks for your help!

3 Likes

Have you tried :WaitForChild() ?

2 Likes

Yes, I also have tried :WaitForChild()

1 Like

Hi! :wave:
PlayerGui may not load so quick on client side, so you need to delay code to wait for instances using :WaitForChild() as @RobotThom1 said.

I tried using :WaitForChild(), I get this warn code: " Infinite yield possible on ‘Players.Player1:WaitForChild(“PlayerGui”)’ "

2 Likes

Huh?
Maybe player was not loaded, have you tried to delay code only after when game is loaded on client side?

So, I start playing the game with the Local Server, 2 Players. And when I spawn, the PlayerGui isnt loaded, I also waited for a few minutes but nothing happened.

Did you tried to playtest and check if you have PlayerGUI inside the player?

I understand, you cannot use a LocalScript to find another player’s PlayerGui the only way you could do this is launching a remoteEvent and use a ServerScript and go from there!

3 Likes

When I play the game in studio normal, I’ve got the PlayerGui and I also have it when I play in the local server, 2 players. I have the PlayerGui everywhere when I’m on that account. You know? When I’m playing 2 players, and I’m on the 1st Player, I have the PlayerGui for the 1st player. but when i check on the 2nd player if the playergui is there for the 1st player, it isnt.

So, do this, when you click on a player fire a RemoteEvent pass all the parameters you need and finish the code in the ServerScript. Other player’s GUIs cannot be accessed locally!

Are you trying to edit another player’s gui with a LocalScript? Their gui would not change if this is the case

Yea, I’m trying to access someone else playergui.

But if I need to make a frame visible, then i can’t make that on the server side.

So then find the player on the ServerSide and use :FireClient() with the players name, and when it gets called to that script do everything you need to!

Example:

Client Side:

local event = game:GetService("ReplicatedStorage").Event
local event2 = game:GetService("ReplicatedStorage").Event2

event:FireServer("PLAYERNAMEHERE", "ANYOTHER PARAMS") -- Fire in your code somewhere

-- function that will be fired back
event2.OnClientEvent:Connect(function(param)
	-- Code here
end)

ServerSide:

local event = game:GetService("ReplicatedStorage").Event
local event2 = game:GetService("ReplicatedStorage").Event2
local players = game:GetService("Players")

event.OnServerEvent:Connect(function(player, PlayerName, otherParams)
	for _, plr in ipairs(players:GetPlayers()) do
		if plr.Name == PlayerName then
			event2:FireClient(plr, otherParams)
		end
	end
end)


So, I now created a remote-event and here’s my code:
Local:

	local player = frame:FindFirstChild("Name")
	local playerWhoAdded = game.Players.LocalPlayer
	game.ReplicatedStorage.Remotes.SendFriendRequestServer:FireServer(player.Text, playerWhoAdded)

Server:

friendRequest.OnServerEvent:Connect(function(player, playerWhoAdded)
	print(tostring(player.Name) .. ", " .. tostring(playerWhoAdded))	
	
	local UI1 = game:FindFirstChild("Players"):FindFirstChild(player.Name)
	local plrGUI = UI1:WaitForChild("PlayerGui")
	local UI = plrGUI:WaitForChild("RequestPopUp")
	local BG = UI:WaitForChild("Background")
	local Template = BG:WaitForChild("Template")
	

	LocalfriendRequest:FireClient(player, Template, playerWhoAdded)
end)

Again Local:

GetRequestFriend.OnClientEvent:Connect(function(player, Template, playerWhoAdded)
	local plr = game.Players.LocalPlayer
	
	print(tostring(player))
	print(tostring(Template))
	-- (tostring(playerWhoAdded))
	
	local cloned = Template:Clone()
	cloned:FindFirstChild("PlayerWhoAdded").Text = plr.Name .. " |"
	cloned:FindFirstChild("PlayerWhoAddedValue").Text = "Value: " .. tostring(plr:FindFirstChild("PlrFolder"):FindFirstChild("PlrValue"):FindFirstChild("PlayerValue").Value)
	cloned.Visible = true

end)

Now it says: Players.Player1.PlayerGui.Events:95: attempt to call missing method ‘Clone’ of string

Not quite, player is always a parameter of a event that was fired, so you won’t need to fire the actual player’s name who is firing it. Here is what I mean.

Client

local player = frame:FindFirstChild("Name")
game.ReplicatedStorage.Remotes.SendFriendRequestServer:FireServer(player.Text)

Server

friendRequest.OnServerEvent:Connect(function(player, otherPlayer) -- player is the player who fired the event, otherPlayer is the first param
	print(tostring(player.Name) .. ", " .. tostring(playerWhoAdded))	
	
	local UI1 = game:FindFirstChild("Players"):FindFirstChild(player.Name)
	local plrGUI = UI1:WaitForChild("PlayerGui")
	local UI = plrGUI:WaitForChild("RequestPopUp")
	local BG = UI:WaitForChild("Background")
	local Template = BG:WaitForChild("Template")
	

	LocalfriendRequest:FireClient(otherPlayer, Template, player)
end)

I think this is what you are trying to accomplish

However, you don’t even need all that stuff in there. You just need to fire to the client the real variables you need.

Unable to cast value to object in this line: LocalfriendRequest:FireClient(playerWhoAdded, Template, player)

What does ‘Template’ mean here?