PlayerGui not existing at the Other Client

Hiya, so I am the developer of the upcoming Chatting App, JustChat. Now, there is an issue which seems pretty weird to me.

For some reason, the chat message I am sending, is displaying on my own client but not on the other one, instead telling me in the console ‘PlayerGui is not a valid member of Players.JCTestingAccount’ and vice versa

Here is an image of it showing both clients:

This is all the code I use for sending it

After enter is pressed, it fires the Text to the server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SendChatMessage = ReplicatedStorage:WaitForChild("SendChatMessage")
local LPlayer = game.Players.LocalPlayer
local textBox = script.Parent

script.Parent.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		SendChatMessage:FireServer(textBox.Text)
	end
end)

Once received, the server then filters the message using game.Chat:FilterStringForBroadcast() (This is mandatory or else ROBLOX might moderate my game).

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SendChatMessage = ReplicatedStorage:WaitForChild("SendChatMessage")

SendChatMessage.OnServerEvent:Connect(function(player, message : string)
	local FilteredMessage = game.Chat:FilterStringForBroadcast(message, player)
	SendChatMessage:FireAllClients(player, FilteredMessage)
end)

Then the server fires the RemoteEvent back to all clients, who in term makes the message visible to everyone

game.ReplicatedStorage.SendChatMessage.OnClientEvent:Connect(function(player : Player, message : string)
	local SenderName = player.Name
	local SenderAvatar = game.Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
	
	local ChatMessageTemplateClone = player.PlayerGui.UI.Main.Chat.ChatMessages.UIListLayout.ChatMessageTemplate:Clone()
	if SenderName == "Hauber_RBLX" then
		ChatMessageTemplateClone.Sender.Text = '<b><font color="#6190ff">[DEV]</font> <font color="#e25f5f">[CHAT MOD]</font></b> '..SenderName
	else
		ChatMessageTemplateClone.Sender.Text = SenderName
	end
	ChatMessageTemplateClone.MessageContent.Text = message
	ChatMessageTemplateClone.Avatar.Image = SenderAvatar
	ChatMessageTemplateClone.Name = SenderName
	ChatMessageTemplateClone.Parent = player.PlayerGui.UI.Main.Chat.ChatMessages
	player.PlayerGui.UI.Main.Chat.ChatMessageInput.Text = ""
end)

Does anyone know why the hell it shows the message on the own client, but not on the other client?

The error is because the client is attempting to get the senders PlayerGui, which isn’t visible to any client but the senders. In the last script you posted, player was passed as a parameter from the server and later on in the script, you assumed player would be the local player, but it is the senders player.

What do I replace it with then?

When you send the parameter player to the client, you can change it to another name like Sender. Assuming that you have defined the local player in the client script, this should work. You might need to change the second to last line.

game.ReplicatedStorage.SendChatMessage.OnClientEvent:Connect(function(Sender : Player, message : string)
	local SenderName = Sender.Name
	local SenderAvatar = game.Players:GetUserThumbnailAsync(Sender.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)

	local ChatMessageTemplateClone = player.PlayerGui.UI.Main.Chat.ChatMessages.UIListLayout.ChatMessageTemplate:Clone()
	if SenderName == "Hauber_RBLX" then
		ChatMessageTemplateClone.Sender.Text = '<b><font color="#6190ff">[DEV]</font> <font color="#e25f5f">[CHAT MOD]</font></b> '..SenderName
	else
		ChatMessageTemplateClone.Sender.Text = SenderName
	end
	ChatMessageTemplateClone.MessageContent.Text = message
	ChatMessageTemplateClone.Avatar.Image = SenderAvatar
	ChatMessageTemplateClone.Name = SenderName
	ChatMessageTemplateClone.Parent = player.PlayerGui.UI.Main.Chat.ChatMessages
	player.PlayerGui.UI.Main.Chat.ChatMessageInput.Text = ""
end)

How am I supposed to clone it into other users’ playergui tho?

Like this?

for _,player in pairs(game.Players:GetPlayers()) do
	ChatMessageTemplateClone.Parent = player.PlayerGui
end

Okay no, I tried that, that method doesn’t work

I’m not sure how you have everything set up, but you shouldn’t need to clone anything from one client to another. Once each client gets the info, (SenderName and message sent) all you need to do is clone the ChatMessageTemplate and fill in all of the info that was received.

You’ll also need a system for moving the old messages up when a new message is received. Right now, you are just stacking all of the messages on top of each other by the looks of it.

If the code I replied with printed an error, post it and I can help fix it.

Yea, but I am not sure what do I parent it to then? Somehow it needs to be added to all the clients. Not just the sender’s client.

Also, the message stacking is handled by a UIListLayout

Oh ok, I see that now. Let me try to break down what you have so far.

  1. Sender fires their message to the server.
  2. Server sends the Sender (Player) and their message to all clients.
  3. Each client clones their own ChatMessageTemplate and uses the received info to fill in their template with SenderName, Avatar, and message.
  4. Each client parents their ChatMessageTemplate to their own chat gui.
  5. UIListLayout does it’s job.

That’s how it is. But still I question how to make it so actually parents the frame to every player’s playergui

Each client is being fired by the server with the info, so you just need to parent the frame to the local players PlayerGui. The local player should be defined at the top of the LocalScript as

local player = game.Players.LocalPlayer

So basically all other clients are defined as game.Players.LocalPlayer or how can I understand that?

Each client has nothing to do with each other. It’s just the senders client sending the info to the server and then the server is sending all of the info to every client that is in the game, including the senders client. So after that, every client has the same exact info. At this point, you parent the gui that you created to the local players PlayerGui.

I don’t know how to explain it any better. If you want to post the whole LocalScript, maybe I can see what’s going on and fix it and you’ll see what I mean.

But wait. How does it know what to clone it in? Don’t I have to specify the player or can I do that via game.Players.LocalPlayer like you said before?

1 Like

You can use game.Player.LocalPlayer to define the local player. This works in any LocalScript since LocalScripts only run on the client, therefore the local player is always known. This only needs to be defined once at the top of the LocalScript. If you already had this, the reason why your script wasn’t working is because you overrode the variable “player” with the “player” you sent from the server, which is why I changed the name to Sender.

3 Likes

Alright, thanks. I’ll try that and let you know if it worked

1 Like

That worked like a charm! Thanks alot for your help!

1 Like

try combinating a localscript with a serverscript… it should work… also you could use remote events

I am already using RemoteEvents. Also I already got it answered.