This is suppose to print if the player inserts an unwanted gui in their PlayerGui, the whitelisted ones (the ones allowed) are located in the table "guis".
But whenever joining the game it always prints "Chat" (which is an actual gui in your PlayerGui), even if you enter a screenGui it'll print that then chat still...
local guis = {"ControlGui", "Chat"} repeat wait() until game.Players for i,v in pairs(game.Players:GetChildren()) do v.PlayerGui.ChildAdded:connect(function(child) for i,v in ipairs(guis) do if guis[i] ~= child.Name then print(child.Name) end end end) end
Sorry about that, was reading too fast. What you need is a function that will search the whole list for a single value. You're iterating over it now but printing every value in guis that doesn't match the child added.
function tableContains(list, find) for _, listItem in ipairs(list) do if listItem == find then return true end end return false end local guis = {"ControlGui", "Chat"} repeat wait() until game.Players for i,v in pairs(game.Players:GetChildren()) do v.PlayerGui.ChildAdded:connect(function(child) if not tableContains(guis, child.Name) then print(child.Name) end end) end
EDIT: To remove the unwanted gui, see the updated script below. This will remove the "Chat" gui since it's not in the whitelist.
spawn() is used here to tell Roblox to call child:Destroy() AFTER then code exits the ChildAdded event handler. It will actually execute it on the next wait(). You can't destroy it in the handler because Roblox is still working with it immediately after the handler. So you have to give Roblox a chance to finish with it first, then destroy it.
function tableContains(list, find) for _, listItem in ipairs(list) do if listItem == find then return true end end return false end local guis = {"ControlGui"} repeat wait() until game.Players for i,v in pairs(game.Players:GetChildren()) do v.PlayerGui.ChildAdded:connect(function(child) if not tableContains(guis, child.Name) then print(child.Name) spawn(function() child:Destroy() end) end end) end
You're determining a bad GUI too early. You want to make sure that all possible whitelisted guis have been ruled out before having the script report the bad GUI.
Create a separate function to run the loop. Have that function return true if the GUI is in the whitelist, or return false when the loop as executed.
local guis = {"ControlGui", "Chat"} repeat wait() until game.Players function check(child) for i,v in pairs(guis) do if v == child.Name then --Use v since that's already the string you want to compare, no sense indexing the table that already pairs an index and value. return true --Return true if the object is in the whitelist. end end return false --We have ruled out all other possibilities, the object is not in the whitelist. end for i,v in pairs(game.Players:GetChildren()) do v.PlayerGui.ChildAdded:connect(function(child) if not check(child) then --Not negates the returned value. If will only take action if the value is true. not true = false, not false = true. print(child.Name) end end) end