Help making a shout command

I am trying to make a command where a player types a message, and it pops up on everyone’s screen. Here is my code:

commands.shout = function(sender,arguments)
	if sender:GetRankInGroup(11635716) >= 10 then
		local message = table.concat(arguments, " ", 1, #arguments)
		
		local Players = game:GetService("Players")
		for i, player in pairs(Players:GetPlayers()) do
			local playerGui = player:WaitForChild("PlayerGui")
			local gui = playerGui:WaitForChild("ShoutGUI")
			local frame = gui:WaitForChild("ShoutFrame")
			local text = frame:WaitForChild("ShoutMessage")
			
			frame.Visible = true
			text.Text = message
			task.wait(10)
			frame.Visible = false
		end
	end
end

I want it to wait 10 seconds before it disappears, but I think it will cause it to wait 10 seconds between each player seeing it, so how do I fix this?

Try putting the wait() outside the loop.

2 Likes

While putting the wait outside of the loops works, it causes extra work to be needed. So, use task.delay.

commands.shout = function(sender,arguments)
	if sender:GetRankInGroup(11635716) >= 10 then
		local message = table.concat(arguments, " ", 1, #arguments)
		
		local Players = game:GetService("Players")
		for i, player in pairs(Players:GetPlayers()) do
			local playerGui = player:WaitForChild("PlayerGui")
			local gui = playerGui:WaitForChild("ShoutGUI")
			local frame = gui:WaitForChild("ShoutFrame")
			local text = frame:WaitForChild("ShoutMessage")
			
			frame.Visible = true
			text.Text = message
			task.delay(10, function()
				frame.Visible = false
			end)
		end
	end
end