I have a script that would show each player a message after a certain time. After the message appears, some code is run. The more players i have in game, the longer it takes. I tested on studio to see what happens with 2 players and i saw that the Gui shows up for one player at a time. I want it to show for all players at the SAME time. Anyone know what i need to do to fix this?
function Message() for i,v in pairs(game.Players:GetPlayers()) do local msg = v:WaitForChild("PlayerGui").ScreenGui.Message message.Visible = true wait(3) message.Visible = false end end while true do wait(20) Message() --Code to run end
Also, this is a normal ServerScript in Workspace.
Any help would be appreciated!
It's because of the wait in the loop inside the function.
I recommend simply using spawn
inside the message function, like so:
function Message() for i,v in pairs(game.Players:GetPlayers()) do spawn(function() --I changed this part to msg instead of message. You might not want this. local msg = v:WaitForChild("PlayerGui").ScreenGui.Message msg.Visible = true wait(3) msg.Visible = false end) end end while true do wait(20) Message() --Code to run end
EDIT:
Scripts running on the server can't access GUIs from the players' playerGui that they did not create and add there, meaning it won't see the ScreenGui, or the Message.
You'll have to use Remote Events.
--// Regular Script local remoteEvent = game.ReplicatedStorage.REMOTEEVENTNAME while true do wait(20) remoteEvent:FireAllClients() end
--// Local Script on the client; like in PlayerGui. local remoteEvent = game.ReplicatedStorage.REMOTEEVENTNAME local gui = DEFINE GUI remoteEvent.OnClientEvent:Connect(function() gui.Visible = true wait(3) gui.Visible = false end)