Answered by
9 years ago Edited 6 years ago
Remotes
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.
Note
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.
Client sending notifications (Scenario one)
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.
Server-side
1 | local RepStorage = game:GetService( "ReplicatedStorage" ) |
2 | local event = RepStorage:WaitForChild( "Event" ) |
4 | event.OnServerEvent:Connect( function (client, request, ...) |
5 | if request = = "notify" then |
6 | event:FireAllClients(request, ...) |
Once OnServerEvent
receives an update from the client, the server will then fire all other clients with the same information passed.
Client-side
1 | event.OnClientEvent:Connect( function (request, ...) |
2 | if request = = "notify" then |
3 | local gui = Instance.new( "ScreenGui" , playerGui) |
4 | local textLabel = Instance.new( "TextLabel" , gui) |
This is what each client will look like. Each client will have a listener that waits for the server to fire their event.
Server sending notifications (Scenario two)
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:
Server-side
01 | local RepStorage = game:GetService( "ReplicatedStorage" ) |
02 | local event = RepStorage:WaitForChild( "Event" ) |
04 | event.OnServerEvent:Connect( function (client, request, ...) |
05 | if request = = "notify" then |
06 | event:FireAllClients(Request, ...) |
11 | event:FireAllClients( "Notify" , "Hello players." ) |
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.