PlayerGui question

Hello fellow scripters!

My question is more of a scripting question rather than a scripting issue:

In critical cases A.K.A when it’s seriously needed 100% can we get the PlayerGui from the Server and not the Client? perhaps we can have some sort of discussion about this too?

I understand that getting the PlayerGui from the Server could maybe cause issues but I think that sometimes it’s not necessary to get it from the Client, also how do we even get the PlayerGui from the Server?

I have tried running this script:

local PlayerGui = game.Players:GetPlayers().PlayerGui
print(PlayerGui)

But the problem is that if you print the PlayerGui variable it will give you nil.

It’s a script not a local script.

1 Like

Use a for loop instead like this:

for i, plr in pairs(game.Players:GetPlayers()) do
    local GUI = plr.PlayerGui
    print(GUI.Name)
end
1 Like

You should never ever handle UI from the server. The server’s changes are meant to replicate to all clients, however by modifying you are changing it for only one player which defeats the purpose. If you actually are in a situation where you need to do it server-sided, then you have done something extremely wrong.

May I redirect you to this amazing post by EllipticCurve_DHE:

1 Like

If you try printing the GUI variable it won’t print at all

I’m not sure why it’s doing this.

I edited the code to fit what you said. Do print(GUI.Name) instead.

1 Like

The purpose of trying to change the UI from the Server and not the Client in the first place is because I want the UI to be changed to everyone effortlessly

You stated that it will replicate to all clients, which is what I need but I’m questioning if I can do it from the Server.

There is an easy way to do this which of course is what I did: a for loop.

1 Like

For some reason once again the GUI variable won’t print, I have tried using your script that you edited, you should test it out.

Just because you can does not mean you should. If the server itself needs to tell clients to enable a GUI, you can use a RemoteEvent to do this instead. A RemoteEvent:FireAllClients() call is all you need! As mentioned in the post I linked, there can be a delay in response times. Each player gets a copy of their own GUIs. Whenever a player clicks a GUI button, they are clicking their own GUI button. It makes no sense for the server to handle something only one player will see.

1 Like

Either I did a typo (because of this stupid autocorrect) or I need to do :WaitForChild() insteadz

1 Like

I understand that I could use a RemoteEvent for this, but that still doesn’t answer my question is it possible to even get the PlayerGui from the server?
If so then how?

Now to answer your second question, imagine that you have a table that holds a list of Generators that have yet to be clicked in order to be removed from the table, and you have a TextLabel with the information of how many Generators left, this is something that would make sense for every player to see in this situation.

I am pretty sure that I and others have implied that yes indeed you can, however there is a negative amount of reason to do so. In other words it was an implication and not a direct answer because you don’t need to know because you shouldn’t do it.

Now for your second part.

SurfaceGuis/BillboardGuis are kind of an exception, however you were talking about PlayerGuis here. Everyone will see the same Surface/BillboardGui. But everyone will see their own ScreenGuis.

1 Like

So you are telling me that SurfaceGuis and BillboardGuis are the only ones that can be handled from the Server and not the Client without having any negative effects or issues that would be caused by it?

The client can modify GUIs there as well. You can use some remotes for that as well so it shows for everyone. But in some cases that might be overkill.

1 Like

Alright thanks for explaining all of this, this will also help people who are looking for these informations.

For anyone who is looking for a quick recap of what you should do and what you shouldn’t do:

Summary:

  • You can handle PlayerGuis from the Server but you shouldn’t do it and you don’t need to know how to do it because it has a negative amount of reasons for it.
  • You can handle SurfaceGuis and BillboardGuis from the Server just as fine as you would handle them from the Client.