DrakerMaker

Untitled

Jul 27th, 2023
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.29 KB | None | 0 0
  1. local module = {}
  2.  
  3. local Storage = {}
  4.  
  5. Storage.__index = {
  6.     Place = function(self, name: string, object: any)
  7.         assert(typeof(name) == 'string', "Name must be a string")
  8.         if self[name] then
  9.             error("Object with name '" .. name .. "' already exists")
  10.         else
  11.             self[name] = object
  12.         end
  13.     end,
  14.  
  15.     Retrieve = function(self, name: string)
  16.         assert(typeof(name) == 'string', "Name must be a string")
  17.         if not self[name] then
  18.             error("Object with name '" .. name .. "' doesn't exist")
  19.         else
  20.             return self[name]
  21.         end
  22.     end,
  23.  
  24.     Remove = function(self, name: string)
  25.         assert(typeof(name) == 'string', "Name must be a string")
  26.         if not self[name] then
  27.             error("Object with name '" .. name .. "' doesn't exist")
  28.         else
  29.             local obj = self[name]
  30.             self[name] = nil
  31.             return obj
  32.         end
  33.     end
  34. }
  35.  
  36. module.Storage = setmetatable({}, Storage)
  37.  
  38. module.Part = {}
  39.  
  40. function module.Part:TracePart(part: Instance, p0: Vector3, p1: Vector3, width: number, height: number)
  41.     assert(typeof(part) == 'Instance', "Part must be an Instance")
  42.     assert(typeof(p0) == 'Vector3', "p0 must be a Vector3")
  43.     assert(typeof(p1) == 'Vector3', "p1 must be a Vector3")
  44.     assert(typeof(width) == 'number', "Width must be a number")
  45.     assert(typeof(height) == 'number', "Height must be a number")
  46.     local Distance = (p0-p1).Magnitude
  47.     part.CFrame = CFrame.new(p0,p1) * CFrame.new(0,0,-Distance/2)
  48.     part.Size = Vector3.new(width,height,Distance)
  49. end
  50.  
  51. module.Math = {}
  52.  
  53. function module.Math:Lerp(a: number, b: number, t: number)
  54.     assert(typeof(a) == 'number', "a must be a number")
  55.     assert(typeof(b) == 'number', "b must be a number")
  56.     assert(typeof(t) == 'number', "t must be a number")
  57.     return a + (b - a) * t
  58. end
  59.  
  60. function module.Math:Flip(num: number)
  61.     assert(typeof(num) == 'number', "num must be a number")
  62.     return -num
  63. end
  64.  
  65. function module.Math:Largest(array: table, place: number)
  66.     assert(typeof(array) == 'table', "array must be a table")
  67.     assert(typeof(place) == 'number', "place must be a number")
  68.  
  69.     local otTable = {table.unpack(array)}
  70.     table.sort(otTable, function(a, b) return a > b end)
  71.     return otTable[place]
  72. end
  73.  
  74. function module.Math:HasDecimal(num: number)
  75.     assert(typeof(num) == 'number', "num must be a number")
  76.     return (math.floor(num) == num)
  77. end
  78.  
  79.  
  80. module.Math.Epsilon = 2.2204460492503131e-16
  81. module.Math.Euler = math.exp(1)
  82. module.Math.GoldenRatio = (1 + math.sqrt(5)) / 2
  83.  
  84. module.Instance = {}
  85.  
  86. function module.Instance.new(class: string, parent: Instance, properties: table)
  87.     assert(typeof(class) == 'string', "Class must be a string")
  88.     assert(typeof(parent) == 'Instance', "Parent must be an Instance")
  89.     assert(typeof(properties) == 'table', "Properties must be a table")
  90.     local object = Instance.new(class, parent)
  91.     if properties ~= nil then
  92.         for key,value in pairs(properties or {["Name"] = "Object"}) do
  93.             object[key] = value
  94.         end
  95.     end
  96.     return object
  97. end
  98.  
  99. module.Sandbox = {}
  100. local curSandbox = {}
  101.  
  102. function module.Sandbox:Run(name: string, funct: any)
  103.     assert(typeof(name) == 'string', "Name must be a string")
  104.     assert(typeof(funct) == 'function', "Code must be function")
  105.     local cor = coroutine.create(funct)
  106.     if curSandbox[name] then
  107.         error("Object with name '" .. name .. "' already exists")
  108.     else
  109.         coroutine.resume(cor)
  110.         curSandbox[name] = cor
  111.         return true
  112.     end
  113. end
  114.  
  115. function module.Sandbox:Yield(name: string)
  116.     assert(typeof(name) == 'string', "Name must be a string")
  117.     if not curSandbox[name] then
  118.         error("Object with name '" .. name .. "' doesn't exist")
  119.     else
  120.         coroutine.yield(curSandbox[name])
  121.         return true
  122.     end
  123. end
  124.  
  125. function module.Sandbox:Resume(name: string)
  126.     assert(typeof(name) == 'string', "Name must be a string")
  127.     if not curSandbox[name] then
  128.         error("Object with name '" .. name .. "' doesn't exist")
  129.     else
  130.         coroutine.resume(curSandbox[name])
  131.         return true
  132.     end
  133. end
  134.  
  135. module.Random = {}
  136. function module.Random:new(seed: number)
  137.     assert(typeof(seed) == 'number', "Seed must be a number")
  138.     local randomObj = Random.new(seed)
  139.     local objs = {}
  140.     function objs:Number(n: number, m: number)
  141.         assert(typeof(n) == 'number', "n must be a number")
  142.         assert(typeof(m) == 'number', "m must be a number")
  143.         return randomObj:NextNumber(n, m)
  144.     end
  145.     function objs:Integer(n: number, m: number)
  146.         assert(typeof(n) == 'number', "n must be a number")
  147.         assert(typeof(m) == 'number', "m must be a number")
  148.         return randomObj:NextInteger(n, m)
  149.     end
  150.     function objs:Table(table: table)
  151.         assert(typeof(table) == 'table', "Table must be a table")
  152.         return table[randomObj:NextInteger(1, #table)]
  153.     end
  154.     function objs:Boolean()
  155.         return (randomObj:NextNumber(0, 1) > 0.5)
  156.     end
  157.     return objs
  158. end
  159.  
  160. module.Asset = {}
  161.  
  162. function module.Asset:Load(assetId: number)
  163.     assert(typeof(assetId) == 'number', "AssetId must be a number")
  164.  
  165.     local InsertService = game:GetService("InsertService")
  166.     local asset
  167.     local success, err = pcall(function()
  168.         asset = InsertService:LoadAsset(assetId)
  169.     end)
  170.  
  171.     if success then
  172.         return asset:GetChildren()[1]
  173.     else
  174.         warn("Failed to load asset: " .. err)
  175.         return nil
  176.     end
  177. end
  178.  
  179. local pool = {}
  180. module.Pool = {}
  181. function module.Pool:Listen(recipient: string, channel: string, funct: any)
  182.     assert(typeof(recipient) == 'string', "Recipient must be a string")
  183.     assert(typeof(channel) == 'string', "Channel must be a string")
  184.     assert(typeof(funct) == 'function', "Callback must be a function")
  185.     if not pool[channel] then
  186.         pool[channel] = {}
  187.     end
  188.     pool[channel][recipient] = funct
  189.     return true
  190. end
  191.  
  192. function module.Pool:Emit(channel: string, data: any)
  193.     assert(typeof(channel) == 'string', "Channel must be a string")
  194.     if not pool[channel] then
  195.         pool[channel] = {}
  196.     end
  197.     for key,funct in pairs(pool[channel]) do
  198.         if typeof(data) == "table" then
  199.             funct(table.unpack(data))
  200.         else
  201.             funct(data)
  202.         end
  203.     end
  204.     return true
  205. end
  206.  
  207. function module.Pool:Disconnect(recipient: string, channel: string)
  208.     assert(typeof(recipient) == 'string', "Recipient must be a string")
  209.     assert(typeof(channel) == 'string', "Channel must be a string")
  210.     assert(pool[channel][recipient], `No recipient named "{recipient}" is listening`)
  211.     pool[channel][recipient] = nil
  212.     return true
  213. end
  214.  
  215. local messagingService = game:GetService("MessagingService")
  216.  
  217. module.Cross = {}
  218. function module.Cross:Publish(channel: string, data: any)
  219.     assert(typeof(channel) == 'string', "Channel must be string")
  220.     messagingService:PublishAsync(channel, data)
  221.     return true
  222. end
  223. function module.Cross:Listen(channel: string, funct: any)
  224.     assert(typeof(channel) == 'string', "Channel must be string")
  225.     assert(typeof(funct) == 'function', "Callback must be function")
  226.     return messagingService:SubscribeAsync(channel, funct)
  227. end
  228.  
  229. module.Debounce = {}
  230. function module.Debounce.new(delayTime: number)
  231.     assert(typeof(delayTime) == 'number', "Delay time must be number")
  232.     local obj = {}
  233.     obj.Allowed = true
  234.     function obj:Pass()
  235.         if obj.Allowed == true then
  236.             task.spawn(function()
  237.                 obj.Allowed = false
  238.                 wait(delayTime)
  239.                 obj.Allowed = true
  240.             end)
  241.             return true
  242.         else
  243.             return false
  244.         end
  245.     end
  246. end
  247.  
  248. function module.Schedule(funct: any, delayTime: number)
  249.     assert(typeof(funct) == 'function', "Callback must be function")
  250.     assert(typeof(delayTime) == 'number', "Delay time must be number")
  251.     local doing = true
  252.     local obj = {}
  253.     function obj.Do()
  254.         if doing == true then
  255.             task.spawn(funct)
  256.             doing = false
  257.         end
  258.     end
  259.     function obj.Cancel()
  260.         doing = false
  261.     end
  262.     task.spawn(function()
  263.         wait(delayTime)
  264.         obj.Do()
  265.     end)
  266.     return obj
  267. end
  268.  
  269. module.Fun = module.Fun or {}
  270. module.Fun.Text = {}
  271.  
  272. function module.Fun.Please(funct: any)
  273.     assert(typeof(funct) == 'function', "What do I do? This isn't a task, or it is no task.")
  274.  
  275.     local randomObj = Random.new()
  276.     local doIt = (randomObj:NextNumber() > 0.5)
  277.     if doIt then
  278.         funct()
  279.         return true, "Sure, why not?"
  280.     else
  281.         return false, "I don't feel like it."
  282.     end
  283. end
  284.  
  285. function module.Fun.Text:Zalgo(text: string)
  286.     assert(typeof(text) == 'string', "Text must be a string")
  287.     local accents = {"̖", "̗", "̘", "̙", "̜", "̝", "̞", "̟", "̠", "̣", "̤", "̥", "̦", "̩", "̪", "̫", "̬", "̭", "̮", "̯", "̰", "̱", "̴", "̵", "̶", "͇", "͈", "͉", "͍", "͎", "͓", "͔", "͕", "͖", "͙", "͚", "̣"}
  288.     local result = ""
  289.     for i = 1, #text do
  290.         result = result .. text:sub(i, i)
  291.         for _ = 1, math.random(1, 10) do
  292.             result = result .. accents[math.random(#accents)]
  293.         end
  294.     end
  295.     return result
  296. end
  297.  
  298. module.Players = {}
  299.  
  300. function module.Players:Hook(player: Instance)
  301.     assert(player:IsA('Player'), "Player must be a Player instance")
  302.  
  303.     local obj = {}
  304.     obj.Player = player
  305.  
  306.     obj.Disconnect = {
  307.         Abrupt = function()
  308.             player:Destroy()
  309.         end,
  310.  
  311.         Kick = function(reason: string)
  312.             player:Kick(reason)
  313.         end
  314.     }
  315.  
  316.     obj.Team = {
  317.         Set = function(teamName: string)
  318.             local team = game:GetService("Teams"):FindFirstChild(teamName)
  319.             if team then
  320.                 player.Team = team
  321.             else
  322.                 error("Team " .. teamName .. " doesn't exist")
  323.             end
  324.         end
  325.     }
  326.    
  327.     local leaderstats = player:FindFirstChild('leaderstats') or Instance.new('Folder')
  328.     leaderstats.Name = 'leaderstats'
  329.     leaderstats.Parent = player
  330.  
  331.     obj.Leaderstats = setmetatable({}, {
  332.         __index = function(_, key)
  333.             return leaderstats:FindFirstChild(key) and leaderstats[key].Value or nil
  334.         end,
  335.         __newindex = function(_, key, value)
  336.             assert(typeof(value) == 'number' or typeof(value) == 'string', "Value must be a number or a string")
  337.             local statObject = leaderstats:FindFirstChild(key)
  338.             if not statObject then
  339.                 statObject = typeof(value) == 'number' and Instance.new('NumberValue') or Instance.new('StringValue')
  340.                 statObject.Name = key
  341.                 statObject.Parent = leaderstats
  342.             end
  343.             statObject.Value = value
  344.         end
  345.     })
  346.  
  347.     return obj
  348. end
  349.  
  350. function module.Players:AddFunction(callback: any)
  351.     local obj = {}
  352.     local connections = {}
  353.  
  354.     task.spawn(function()
  355.         for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
  356.             callback(player)
  357.         end
  358.     end)
  359.  
  360.     task.spawn(function()
  361.         local connection = game:GetService("Players").PlayerAdded:Connect(function(player)
  362.             callback(player)
  363.         end)
  364.         table.insert(connections, connection)
  365.     end)
  366.  
  367.     function obj:Disconnect()
  368.         for _, connection in ipairs(connections) do
  369.             connection:Disconnect()
  370.         end
  371.     end
  372.  
  373.     return obj
  374. end
  375.  
  376. module.Throttling = {}
  377.  
  378. function module.Throttling.new(timeFrame: number, maxCalls: number)
  379.     assert(typeof(timeFrame) == 'number', "Time frame must be a number")
  380.     assert(typeof(maxCalls) == 'number', "Max calls must be a number")
  381.  
  382.     local obj = {}
  383.     obj.TimeFrame = timeFrame
  384.     obj.MaxCalls = maxCalls
  385.     obj.CallCount = 0
  386.     obj.LastCallTime = 0
  387.  
  388.     function obj:Pass()
  389.         local currentTime = tick()
  390.         if self.LastCallTime == 0 or (currentTime - self.LastCallTime) > self.TimeFrame then
  391.             -- Reset call count if time frame has passed since last call
  392.             self.CallCount = 0
  393.         end
  394.  
  395.         if self.CallCount < self.MaxCalls then
  396.             -- Call count is below the limit, allow the call
  397.             self.CallCount = self.CallCount + 1
  398.             self.LastCallTime = currentTime
  399.             return true
  400.         else
  401.             -- Call count has reached the limit, don't allow the call
  402.             return false
  403.         end
  404.     end
  405.  
  406.     return obj
  407. end
  408.  
  409. module.Timer = {}
  410.  
  411. function module.Timer.new(time: number, tickInterval: number)
  412.     assert(typeof(time) == 'number', "Time must be a number")
  413.     assert(typeof(tickInterval) == 'number', "Tick interval must be a number")
  414.  
  415.     local obj = {}
  416.     obj.TimeLeft = time
  417.     obj.TickInterval = tickInterval
  418.     obj.Running = false
  419.     obj.Tick = function() end -- Default to empty function
  420.     obj.Ended = function() end -- Default to empty function
  421.  
  422.     function obj:Start()
  423.         assert(not self.Running, "Timer is already running")
  424.         self.Running = true
  425.         task.spawn(function()
  426.             while self.Running and self.TimeLeft > 0 do
  427.                 self.Tick(self.TimeLeft)
  428.                 wait(self.TickInterval)
  429.                 self.TimeLeft = self.TimeLeft - self.TickInterval
  430.             end
  431.             if self.Running then -- Only call Ended if the timer wasn't cancelled
  432.                 self.Ended()
  433.             end
  434.         end)
  435.     end
  436.  
  437.     function obj:Cancel()
  438.         self.Running = false
  439.     end
  440.  
  441.     return obj
  442. end
  443.  
  444. module.Array = {}
  445.  
  446. function module.Array:Limit(array: table, lowerBound: number, upperBound: number)
  447.     assert(type(array) == 'table', "Array must be a table")
  448.     assert(typeof(lowerBound) == 'number', "Lower bound must be a number")
  449.     assert(typeof(upperBound) == 'number', "Upper bound must be a number")
  450.  
  451.     local result = {}
  452.     for i = 1, #array do
  453.         if array[i] >= lowerBound and array[i] <= upperBound then
  454.             table.insert(result, array[i])
  455.         end
  456.     end
  457.  
  458.     return result
  459. end
  460.  
  461. function module.Array:ForEach(array: table, callback: any)
  462.     assert(type(array) == 'table', "Array must be a table")
  463.     assert(typeof(callback) == 'function', "Callback must be a function")
  464.     for index, value in ipairs(array) do
  465.         callback(value)
  466.     end
  467.     return true
  468. end
  469.  
  470. local base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  471.  
  472. module.Misc = {}
  473. module.Misc.Base64 = {}
  474.  
  475. function module.Misc.Base64:Encode(data)
  476.     data = tostring(data)
  477.     return ((data:gsub('.', function(x)
  478.         local r, b = '', x:byte()
  479.         for i = 8, 1, -1 do r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0') end
  480.         return r;
  481.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  482.         if (#x < 6) then return '' end
  483.         local c = 0
  484.         for i = 1, 6 do c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0) end
  485.         return base64Chars:sub(c + 1, c + 1)
  486.     end)..({ '', '==', '=' })[#data % 3 + 1])
  487. end
  488.  
  489. function module.Misc.Base64:Decode(data)
  490.     data = tostring(data)
  491.     data = data:gsub('[^'..base64Chars..'=]', '')
  492.     return (data:gsub('.', function(x)
  493.         if (x == '=') then return '' end
  494.         local r, f = '', (base64Chars:find(x) - 1)
  495.         for i = 6, 1, -1 do r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and '1' or '0') end
  496.         return r;
  497.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  498.         if (#x ~= 8) then return '' end
  499.         local c = 0
  500.         for i = 1, 8 do c = c + (x:sub(i, i) == '1' and 2 ^ (8 - i) or 0) end
  501.         return string.char(c)
  502.     end))
  503. end
  504.  
  505. local DataStoreService = game:GetService("DataStoreService")
  506.  
  507. module.DataStore = {}
  508.  
  509. function module.DataStore:GetStore(storeName: string)
  510.     assert(typeof(storeName) == 'string', "Store name must be a string")
  511.     local dataStore = DataStoreService:GetDataStore(storeName)
  512.     local store = {}
  513.  
  514.     function store:Get(key: string, value: any)
  515.         local success, result = pcall(function()
  516.             return dataStore:GetAsync(key)
  517.         end)
  518.         if success then
  519.             return result
  520.         else
  521.             warn("Failed to get data for key: " .. key)
  522.         end
  523.     end
  524.  
  525.     function store:Set(key: string, value: any)
  526.         local success, result = pcall(function()
  527.             dataStore:SetAsync(key, value)
  528.         end)
  529.         if success then
  530.             return true
  531.         else
  532.             warn("Failed to set data for key: " .. key)
  533.             return false
  534.         end
  535.     end
  536.  
  537.     return store
  538. end
  539.  
  540. local function tryRequire(moduleScript)
  541.     local status, result = pcall(require, moduleScript)
  542.     if status then
  543.         return result
  544.     else
  545.         print(string.format("Failed to require %s: %s", moduleScript:GetFullName(), tostring(result)))
  546.         return nil
  547.     end
  548. end
  549.  
  550. local loadstringModule = tryRequire(script.Loadstring)
  551.  
  552.  
  553. function module.Loadstring(str: string, env: table)
  554.     assert(typeof(str) == 'string', "Code must be string")
  555.     if env then
  556.         assert(typeof(env) == 'table', "Environment must be table")
  557.     end
  558.     return loadstringModule(str, env)
  559. end
  560.  
  561. return module
Add Comment
Please, Sign In to add comment