I’m trying to create a text label controlled by the server.
I have this script, but I don’t think this is the most efficient way to do it.
for i,v in pairs (game.Players:GetChildren()) do
v.PlayerGui.something.TextLabel.Text = "example"
end
I’m trying to create a text label controlled by the server.
I have this script, but I don’t think this is the most efficient way to do it.
for i,v in pairs (game.Players:GetChildren()) do
v.PlayerGui.something.TextLabel.Text = "example"
end
Since I am guessing the script you provided is handled on the server, you could use remote events/function and fire all clients, but your method isn’t necessarily inefficient
Instead of GetChildren(), use GetPlayers(). It’s usually bad practice to use GetChildren instead of the specialized function for retrieving players.
Edit: Also it isn’t necessarily inefficient. Server can access PlayerGui just fine, so there wouldn’t be any need to use remotes to fire to the client.
From a server script, you can fire a function for all clients using a RemoteEvent
In a server script:
game.ReplicatedStorage.RemoteEvent:FireAllClients()
In a local script:
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
game.Players.LocalPlayer.PlayerGui.something.TextLabel.Text = "example"
end)
Hello! I think this isn’t inefficient, you shoud use game.Players:GetPlayers(), and also, to agree with Jay, You should use FireAllClients
What specifically are you trying to do? What is your exact use case that needs to update a Gui for everyone?
Best case scenario, its something that can be controlled by the client itself and you don’t need to incur any network overhead by using a RemoteEvent contrary to responses above. You should always aim to keep Gui-based work on the client-side. If it can’t be done from the client itself, use a RemoteEvent.
I’m creating a “camping” game, so I need to use it for the dialogs.
Understandable. In that case, refer to the replies above. You can still handle this without RemoteEvents in a fashion but I’ll assume your story is mostly controlled server-side. You will then be looking to send the current narration to all clients and then have the client handle the incoming data from the server.
for i,v in pairs(game.Players:GetChildren()) do
if v:IsA(“Player”) then
v.PlayerGui.ScreenGui.TextLabel.Text = “Text”
end
end
Here’s to express the beauty of RemoteEvents and listening.
Assuming you have a premade TextLabel in StarterGui.
First create a RemoteEvent and place it in ReplicatedStorage
, lets call it ListeningRemote.
Assuming the text is controlled by the server, create a LocalScript anywhere somewhat similar to this.
local Replicated = game:GetService("ReplicatedStorage")
function post(text)
Replicated.ListeningRemote:FireServer(text)
end
if your conditions then
post(your text)
end
Now, inside the TextLabel, make a Script
:
local Replicated = game:GetService("ReplicatedStorage")
Replicated.ListeningRemote.OnServerEvent:connect(function(plr, text)
script.Parent.Text = text
end)
I can help further if needed. DM at arvin#6145
Hope I helped, happy new year.
What would happen if the player leaves while this happens?