Hello Developers! I’m trying to do a script, I hope you can help me, I will now answer these questions, this is related to UIs.
What do you want to achieve? Keep it simple and clear!
I want to do a script. When you press a button it does visible a UI for all the players same to desactivate it.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried searching in the Devforum I found a topic, but I need more help with the script.
I’m trying to do a script of a Gui that it comes visible for all the players when you click a button, with a RemoteEvent, so it fires the RemoteEvent and the Gui comes visible to all the players. I hope you can help me.
Sometimes, A RemoteEvent relies on a FireAllClients. Most of the time, RemoteEvents are used for a lot. Since for a ScreenGUI, you’d need to make a remoteevent and make the event fireallclients, in order to make it appear.
If you are trying to do :FireAllClients() on a client it won’t work. You will need to send the request to make a GUI visible to the server then have the server run :FireAllClients().
Instead of using :FireAllClients, what you could do instead is put the GUI in the StarterGUI, where if the player joins the game, it will be added to their PlayerGui. Now make this ScreenGUI invisible, then fire the server, then on the script what you could do is:
game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(Player)
local players = game:GetService("Players")
for _, player in ipairs(players:GetChildren()) do
local GUI = player.PlayerGui:FindFirstChild("GuiNameHere")
if GUI ~= nil then
GUI.Visible = true
end
end
end)
The code mentioned above is supposed to be in a regular script. Maybe place it in ServerScriptService.
It runs a loop through the players and finds the aforementioned GUI that you want to make it visible to everyone, then if it exists (exists, but not visible), then it will make it visible for everyone. Though you will have to use a Remote Event and place it in ReplicatedStorage, then fire it from a Local Script (which is where the button is)
If it doesn’t work, reply with the errors in the output window.
So, this depends on what exactly you mean. When you say the above, do you mean only when you specifically press it, or when any player in the game presses it?
For the latter, here’s a solution:
Put 2 RemoteEvents in ReplicatedStorage
Have a script placed in ServerScriptService
Place a localscript somewhere inside StarterGui or inside of any folder/gui inside of it
In the script, do something like this:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Variables
local remoteEvent1 = ReplicatedStorage:WaitForChild("remoteEvent1")
local remoteEvent2 = ReplicatedStorage:WaitForChild("remoteEvent2")
remoteEvent1.OnServerEvent:Connect:(function() -- Checks for when remoteEvent1 is received
remoteEvent2:FireAllClients() -- Fires remoteEvent2 to all clients
end)
And in the LocalScript have something like:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Variables
local remoteEvent1 = ReplicatedStorage:WaitForChild("remoteEvent1")
local remoteEvent2 = ReplicatedStorage:WaitForChild("remoteEvent2")
local button = -- Path to the gui button
local gui = -- Path to the gui you want to make visible/invisible
-- When button is clicked, it fires remoteEvent1 to the server
button.MouseButton1Click:Connect(function()
remoteEvent1:FireServer()
end)
-- When remoteEvent2 is received, it will make the gui visible
remoteEvent2.OnClientEvent:Connect(function()
gui.Visible = true
end)
How this will work is, if anyone in the game clicks the button the following will happen in this order:
The client clicks the button
The client will fire remoteEvent1 to the server
The server will receive remoteEvent1
The server will then fire remoteEvent2 to all clients
All clients will receive remoteEvent2
All clients will make the specified gui visible
Let me know if this isn’t what you want, or just how it goes, or any questions!
--Server Script
local remote = path.to.remote
remote.OnServerEvent:Connect(function(player, ...)
local args = {...}
--maybe do some checks if the player can fire the event
remote:FireAllClients(unpack(args))
end)
--Client
local remote = path.to.remote
local guiToOpen = path.to.gui
local guiOpenDebounce = false
--Lets say we have a button as the parent of this
script.Parent.MouseButton1Click:Connect(function()
remote:FireServer()
end)
remote.OnClientEvent:Connect(function()
if guiOpenDebounce then
return -- so it can only open and close, instead of opening twice and closing twice, etc.
end
guiOpenDebounce = true
guiToOpen.Visible = true
wait(4)
guiOpenDebounce = false
guiToOpen.Visible = false
end)