I want to make a script that will send a gui (notification) to everyone in the server. How do I do that?
I recommend using RemoteEvents
for doing this. Whether it's the server notifying all players, or a player notifying all players, all GUI input should always be handled locally. I'll give an example of both scenarios from above.
The local script will actually be doing all the work. The only thing we need the server to do is fire the remotes, which will fire the client-sided events.
Let's say I have a local script inside a GUI button, and when I push the button, a GUI will appear on everyone's screen. First, let's see what this should look like on the server-side.
local RepStorage = game:GetService("ReplicatedStorage") local event = RepStorage:WaitForChild("Event") event.OnServerEvent:Connect(function(client, request, ...) if request == "notify" then event:FireAllClients(request, ...) end end)
Once OnServerEvent
receives an update from the client, the server will then fire all other clients with the same information passed.
event.OnClientEvent:Connect(function(request, ...) if request == "notify" then local gui = Instance.new("ScreenGui", playerGui) local textLabel = Instance.new("TextLabel", gui) textLabel.Text = ... end end)
This is what each client will look like. Each client will have a listener that waits for the server to fire their event.
This scenario involves the server sending the notifications to all players, instead of an individual client sending a notification to all players.
There's really no change here. We can simply just modify the code from the first server script, and should have something that looks like this:
local RepStorage = game:GetService("ReplicatedStorage") local event = RepStorage:WaitForChild("Event") event.OnServerEvent:Connect(function(client, request, ...) if request == "notify" then event:FireAllClients(Request, ...) end end) while wait(5) do event:FireAllClients("Notify", "Hello players.") end
This may seem a bit complicated if you're not familiar with remotes, but I strongly recommend using them. Here's a tutorial on how to use remote events if you're interested.
Anyway, hope it helped. Let me know if you have any questions.