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!
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.
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!
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!
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!
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)
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.