darraghd493

Basic Protect Gui

Jan 3rd, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | Source Code | 0 0
  1. --[[
  2.     Basic Protect Gui by darraghd493
  3.     This is a basic module which prevents a gui from being indexed, offering basic protection.
  4. ]]
  5.  
  6. local library = {
  7.     protected = {},
  8.     connections = {} -- { gui = connection }
  9. }
  10.  
  11. local oldNc;
  12. oldNc = hookmetamethod(game,"__namecall",function(...)
  13.     local args = {...}
  14.     if not checkcaller() and args[1] == game and getnamecallmethod() == "FindFirstChild" and args[3] == true then
  15.         for i, v in pairs(library.protected) do
  16.             if v.Name == args[2] then
  17.                 return nil
  18.             end
  19.         end
  20.     end
  21.     return oldNc(...)
  22. end)
  23.  
  24. function library.protect(gui: ScreenGui)
  25.     table.insert(library.protected, gui)
  26.  
  27.     for i, v in pairs(gui:GetDescendants()) do
  28.         table.insert(library.protected, v)
  29.     end
  30.  
  31.     library.connections[gui] = gui.DescendantAdded:Connect(function(v)
  32.         table.insert(library.protected, v)
  33.  
  34.         -- Ensure the connection is not active when the gui is not protected
  35.         if not table.find(library.protected, gui) then
  36.             library.connections[gui]:Disconnect()
  37.         end
  38.     end)
  39. end
  40.  
  41. function library.unprotect(gui: ScreenGui)
  42.     table.remove(library.protected, table.find(library.protected, gui))
  43.  
  44.     for i, v in pairs(gui:GetDescendants()) do
  45.         table.remove(library.protected, table.find(library.protected, v))
  46.     end
  47.  
  48.     if library.connections[gui] then
  49.         library.connections[gui]:Disconnect()
  50.     end
  51. end
  52.  
  53. return library
Advertisement
Add Comment
Please, Sign In to add comment