Advertisement
Guest User

source for TRIGGER

a guest
Oct 2nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. local TRIGGER = {}
  2. local triggers = {}
  3.  
  4. local chars = {
  5.     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
  6.     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3",
  7.     "4", "5", "6", "7", "8", "9",
  8. }
  9.  
  10. local function generate_id()
  11.     local random = math.random
  12.     local template = "xxxxxxx-xxxx-xxxx-yxxxx"
  13.     return string.gsub(template, "[xy]", function(c)
  14.         local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
  15.         return string.format("%x", v)
  16.     end)
  17. end
  18.  
  19. --[[
  20.     params {
  21.         properties: 'table'
  22.         _repeat:    'bool' -> true: touched event fires infinitely at the rate of the given cooldown ; false: touched event fires once
  23.         callback:   'function' -> gets called when the trigger object gets touched
  24.         part:       Instance of class 'BasePart' -> the part to turn into a trigger object
  25.     }
  26.    
  27.     TRIGGER:new()        -> Creates trigger object from scratch with given properties
  28.     *NOTICE* When using TRIGGER:new() you need to parent the part to workspace yourself.
  29.    
  30.     trigger:hook()       -> Hooks the trigger to the part
  31.     trigger:unhook()     -> Unhooks the trigger from the part
  32.     trigger:initialize() -> Initializes the trigger *fires when the trigger object gets called*
  33.     trigger:destroy()    -> Destroys the part along with the trigger object
  34.     trigger:reset()      -> Resets the trigger and leaves it unhooked
  35.    
  36.     <---------------------------->
  37.    
  38.     Example with part:
  39.     local TRIGGER = require(PATH_TO_MODULE)
  40.  
  41.     local trigger = TRIGGER(PATH_TO_BASEPART, false, function()
  42.         print("triggered")
  43.     end)
  44.    
  45.     <---------------------------->
  46.    
  47.     Example without part:
  48.     local TRIGGER = require(PATH_TO_MODULE)
  49.     local properties = {
  50.         Name = "New Trigger";
  51.         Position = Vector3.new(0, 10, 0);
  52.         Anchored = true;
  53.         CanCollide = false;
  54.         Size = Vector3.new(10, 10, 10);
  55.         Parent = workspace;
  56.     }
  57.    
  58.     TRIGGER:new(properties, false, function()
  59.         print("new trigger")
  60.     end)
  61.    
  62.     <---------------------------->
  63. ]]
  64.  
  65. function TRIGGER:new(properties, _repeat, callback)
  66.     local trigger_part = Instance.new("Part")
  67.     for property,value in pairs(properties) do
  68.         trigger_part[property] = value
  69.     end
  70.     self(trigger_part, _repeat, callback)
  71. end
  72.  
  73. function TRIGGER:getTriggers()
  74.     return triggers
  75. end
  76.  
  77. setmetatable(TRIGGER, {
  78.     __call = function(self, part, _repeat, callback)
  79.         callback = callback or function() end
  80.        
  81.         local trigger = {
  82.             part = part;
  83.             _repeat = _repeat;
  84.             triggered = {};
  85.             cooldowns = {};
  86.             cooldown = 2;
  87.             state = "unhooked";
  88.             connection = nil;
  89.             id = generate_id();
  90.            
  91.             hook = function(self)
  92.                 self.state = "hooked"
  93.                 if not self.connection then
  94.                     self.connection = part.Touched:Connect(function(hit)
  95.                         pcall(function()
  96.                             local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
  97.                             if self._repeat then
  98.                                 while table.find(self.part:GetTouchingParts(), hit) do
  99.                                     if os.time() - self.cooldowns[player] >= self.cooldown then
  100.                                         self.cooldowns[player] = os.time()
  101.                                         callback(player)
  102.                                     end
  103.                                     wait()
  104.                                 end
  105.                             else
  106.                                 if not self.triggered[player] then
  107.                                     self.triggered[player] = true
  108.                                     for i,v in pairs(self.triggered) do print(i,v) end
  109.                                     callback(player)
  110.                                 end
  111.                             end
  112.                         end)
  113.                     end)
  114.                 end
  115.             end;
  116.            
  117.             unhook = function(self)
  118.                 self.state = "unhooked";
  119.                 self.connection:Disconnect()
  120.                 self.connection = nil
  121.             end;
  122.            
  123.             initialize = function(self)
  124.                 table.insert(triggers, self)
  125.                 for _,player in pairs(game:GetService("Players"):GetPlayers()) do
  126.                     self.cooldowns[player] = os.time()
  127.                 end
  128.                 game:GetService("Players").PlayerAdded:Connect(function(player)
  129.                     self.cooldowns[player] = os.time()
  130.                 end)
  131.                 self:hook()
  132.             end;
  133.            
  134.             reset = function(self)
  135.                 if self.state == "hooked" then self.connection:Disconnect() end
  136.                 self.connection = nil
  137.                 self.state = "unhooked"
  138.                 self.triggered = {}
  139.             end;
  140.            
  141.             destroy = function(self)
  142.                 local i = table.find(triggers, self)
  143.                 table.remove(triggers, i)
  144.                 self.part:Destroy()
  145.             end;
  146.         }
  147.        
  148.         trigger:initialize()
  149.        
  150.         return trigger
  151.     end
  152. });
  153.  
  154. return TRIGGER
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement