minimic2002

MetaTable Example

Feb 4th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. local PlayerHandlers = {}
  2. local Players = game:GetService("Players")
  3. local DSS = require(game:FindFirstChild("DSS",true))
  4.  
  5. ---------------------------------------------------// Initial Setup
  6. local PlayerHandler = {}
  7.  
  8. function PlayerHandler.SetUp(Player)
  9. if not PlayerHandlers[Player] then
  10.  
  11. local instance = setmetatable({
  12. ["Player"] = Player,
  13. ["Connections"] = {},
  14. ["Character"] = Player.Character or Player.CharacterAdded:Wait(),
  15. },{
  16. __Index = PlayerHandler
  17. })
  18. PlayerHandlers[Player] = instance
  19. return instance
  20. end
  21. end
  22.  
  23. function PlayerHandler:SetupConnection(Name: "String",Connection: "Event",Function: "Function")
  24. self["Connections"][Name] = Connection:Connect(Function)
  25. end
  26.  
  27.  
  28. ---------------------------------------------------// Initial Setup
  29. for _,v in pairs(Players) do
  30. PlayerHandler.SetUp(v)
  31. end
  32.  
  33. ---------------------------------------------------// NewPlayer
  34. Players.PlayerAdded:Connect(function(Player)
  35. PlayerHandler.SetUp(Player)
  36. end)
  37.  
  38. Players.PlayerRemoving:Connect(function(Player)
  39. for _,v in pairs(PlayerHandlers[Player]["Connections"]) do
  40. v:Disconnect()
  41. end
  42. end)
Advertisement
Add Comment
Please, Sign In to add comment