Hi! I want to show a GUI to every player in the server after a player says ':m' in the chat. The GUI shows up but it only shows to one player, and after the the GUI disappears it shows up to the next player, and so on. Anyone has any idea why?
Script:
local Players = game:GetService("Players")
local messageActivator = ":m"
local MESSAGE = ""
local MESSAGER = ""
local function onPlayerChatted(player, message, recipient)
if message:sub(1, messageActivator:len()):lower() == messageActivator:lower() then
if message:sub(1, messageActivator:len()):lower() == messageActivator:lower() then
MESSAGE = message:sub(messageActivator:len() + 1)
MESSAGER = player.Name
end
end
local function onPlayerAdded(player)
player.Chatted:connect(function (...)
onPlayerChatted(player, ...)
for i,v in pairs(game.Players:GetPlayers()) do
v.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Message").Visible = true
v.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Message"):WaitForChild("Name").Text = MESSAGER
v.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Message"):WaitForChild("Message").Text = MESSAGE
wait(3)
v.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Message").Visible = false
end
end)
end
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

2 comments
It's because of the wait(3), since you're going through each player it would go to the first player, wait three seconds, second and wait, third and wait. You'd have to make your script simulate a thread with coroutine or spawn(). However, what you're doing is bad practice as Guis should be handled on the client end. I'd recommend looking into RemoteEvents.
Remote events are indeed the optimal method to achieve your desired result.