Hello there guys, I have a script where I want a GUI to display saying "Not enough players" if there aren't a certain amount of players in the game, while also removing the GUI if enough players are met. The script is in ServerScriptService, and with FilteringEnabled set to on, scripts in ServerScriptService are not able to read PlayerGuis. So I have to use a for i, v to clone a GUI into the player. The problem with this is that with the while true do running, it is constantly cloning GUIs into the player and the game becomes INCREDIBLY laggy.
How can I make it so while true do only runs ONCE and then restarts the loop? Do I include a break somewhere?
while true do wait()
if game.Players.NumPlayers >= MinPlayers then
for i, v in pairs(game.Players:GetChildren()) do
local playergui = v:FindFirstChild("PlayerGui")
if playergui then
local somegui = playergui:FindFirstChild("playercheckgui")
if somegui then
somegui:Destroy()
print("Destroyed first GUI in PlayerGui")
end
end
end
else
for i, v in pairs(game.Players:GetChildren()) do
local player = v:FindFirstChild("PlayerGui")
local screengui = Instance.new('ScreenGui', player)
screengui.Name = "playercheckgui"
---- Create textLabel
local imageLabel = Instance.new('ImageLabel', player.playercheckgui)
imageLabel.Position = UDim2.new(0.3, 0, 0, 0)
imageLabel.Size = UDim2.new(0.35, 0, 0.15, 0)
imageLabel.BackgroundTransparency = 1
end
end
The other alternative is to place this script in Workspace and call it a day? But that is bad security wise, yes?

3 comments
Very interesting how you manipulated the GUI from the clientside way instead of the server side way I was doing. I was not aware it cold be done like that, very neat indeed, Thanks! For you or anyone wondering what I did to mitigate the issue before I saw your reply, I kept the while true do loop and added a debounce past the "else", something like this if not debounce then debounce = true and
Infinite loops are bad. You should consider using Event callbacks.
@Link150 ...apparently I am not supposed to be using playeradded in a localscript https://forum.roblox.com/Forum/ShowPost.aspx?PostID=190238127 strange. Idk though