Enabling / disabling PlayerGui from a script

Hi! So recently I was trying to make a menu type thing, that has a cutscene with a chat gui thing. I have the player, clicking a button, which fires and event. So I have the rest of the code in the script that listened for the event to be fired. How should I have the game disable the menu GUI, since as far as I know, you can’t access PlayerGui from a regular script “game.Players.LocalPlayer.PlayerGui” <---- doesn’t work.

any help is very much appreciated, thanks!

Once the cutscene starts, fire a remote event to a new local script and disable the PlayerGui there.

where should I put the local script? In the remote event?

The remote event goes in ReplicatedStorage, the local script goes in StarterPlayerScripts.

OnServerEvent can only be used on the server <— the error I got after putting the local script in starterplayerscripts

so basically the local script rn is listening for the event to be fired, and once fired it disables the Menu gui…

In a local script, you use OnClientEvent instead of OnServerEvent.

Hi! I tried that, it gets rid of the error, but now nothing is happening----

Would you mind showing me your code on the server and on the client? Using a remote event to the client should work.

Sure! Its VERY messy right now, because I’ve been trying a bunch of different things, I’ll clean it up later.

this is a local script under the GUI button

local button = script.Parent
local event = replicatedStorage:WaitForChild("StartGame")
local players = game:GetService("Players")



button.MouseButton1Click:Connect(function()
	event:FireServer(1)
	
end)

and this is the code that I have in the local script that I put in starterplayerscripts

local players = game.Players

local localPlayer = players.LocalPlayer

local gui = localPlayer:WaitForChild("PlayerGui")

local replicatedStorage = game:GetService("ReplicatedStorage")
local chatGui = game.StarterGui.ChatGui.Frame
local sleepGui = game.StarterGui.Sleeping
local menu = game.StarterGui.MenuGui
local event = replicatedStorage:WaitForChild("StartGame")



event.OnClientEvent:Connect(function(player, num)
	print(player.Name.." fired "..event.Name)
	gui.Sleeping.Enabled = true
	gui.MenuGui.Enabled = false
	while gui.Sleeping.Frame.BackgroundTransparency > 0 do
		gui.Sleeping.Frame.BackgroundTransparency -= 0.1
	end
end)

Again, please ignore all the messy, out of place stuff, I haven’t had the chance to clean it up yet, since I keep trying different things, trying to find what works.

Are you trying to enable the sleeping GUI or disable it? If you are trying to disable it then you should probably add a wait(0.1) inside your while loop or else the transparency is going to change instantly

A server needs to process the remote event and send it back to the client. Add this to a regular script in ServerScriptService:

local event = game.ReplicatedStorage:WaitForChild("StartGame")

event.OnServerEvent:Connect(function(player,num)
	event:FireClient(player,num)
end)

Also, there is no need to keep the player parameter on the local script in StarterPlayerScripts since the server needs that to determine which client to send it to.

Im trying to enable the sleeping gui - basically I’m gonna have it just be a black screen that slowly becomes transparent, almost as if you were waking up (or atleast thats what I hope it looks like)

<— this is all after the player clicks the play on the menu button

I see what you are doing here now, the problem is that you are changing the starter GUI not the player’s GUI. So try this:

event.OnClientEvent:Connect(function(player, num)
	print(player.Name.." fired "..event.Name)
	
	local gui = player:WaitForChild("PlayerGui")
	gui.Sleeping.Enabled = true
	gui.MenuGui.Enabled = false
	while gui.Sleeping.Frame.BackgroundTransparency > 0 do
		gui.Sleeping.Frame.BackgroundTransparency -= 0.1
	end
end)

Hi! So after changing these, it works now! So thank you for that!

Also, how should I go about having the sleeping gui slowly increase transparency (going from black to clear), I was thinking I could use a repeat until, but I’m not so sure.

Something like this:

while gui.Sleeping.Frame.BackgroundTransparency < 1 do
		gui.Sleeping.Frame.BackgroundTransparency += 0.1
end

just exactly the same way you did but backwards

oohhhhh thanks! I can’t believe I made such a simple mistake… Oh well, it happens to everyone I suppose… Thank you!

1 Like

The PlayerGui instance of a player should never be handled by the server, in this scenario I’d recommend handling everything inside of a local script (you mitigate the necessity of firing the server and then having the server refire the client which will just cause unnecessary latency issues). The PlayerGui object and its descendants all replicate to the client (and are client specific) as such they can be easily read from and manipulated from the client through the use of local scripts (modifications to a player’s PlayerGui do not need to be replicated to the server).

1 Like