Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Still prints "chat" even though chat is on the whitelist?

Asked by 7 years ago
Edited 7 years ago

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

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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
0
Yes sorry, just fixed that, it still print chat though. UnleashedGamers 257 — 7y
0
I found a problem, possibly. I was testing this out in a local server, and I was still able to insert a gui into my PlayerGui without getting kicked. UnleashedGamers 257 — 7y
0
You just asked to print the unwanted GUI. I'll edit to add this new request. GixxerK3 20 — 7y
0
It' still allows me to insert the gui in-game, when not on whitelist and it does not destroy it. UnleashedGamers 257 — 7y
0
Does it give an error? Does the code I posted work for you for the Chat gui? GixxerK3 20 — 7y
Ad
Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Problem

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.


Solution

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.


Final Script

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

Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to post them in the comments below.
0
Shouldn't line 6 be "if v == child.Name then". Your logic is inverted. If you take "ControlGui" out of guis, it will still print "Chat" GixxerK3 20 — 7y
0
Yeah this isn't working. UnleashedGamers 257 — 7y
0
My logic isn't inverted, more of I just copy and pasted the code without checking. I made the edit. I make typos, I make mistakes, everyone is human. M39a9am3R 3210 — 7y
0
I didn't say that to criticize you at all. The logic of that if statement was literally inverted, lol. I was just letting you know so you could edit your answer which was otherwise the same as mine. GixxerK3 20 — 7y

Answer this question