Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- local Storage = {}
- Storage.__index = {
- Place = function(self, name: string, object: any)
- assert(typeof(name) == 'string', "Name must be a string")
- if self[name] then
- error("Object with name '" .. name .. "' already exists")
- else
- self[name] = object
- end
- end,
- Retrieve = function(self, name: string)
- assert(typeof(name) == 'string', "Name must be a string")
- if not self[name] then
- error("Object with name '" .. name .. "' doesn't exist")
- else
- return self[name]
- end
- end,
- Remove = function(self, name: string)
- assert(typeof(name) == 'string', "Name must be a string")
- if not self[name] then
- error("Object with name '" .. name .. "' doesn't exist")
- else
- local obj = self[name]
- self[name] = nil
- return obj
- end
- end
- }
- module.Storage = setmetatable({}, Storage)
- module.Part = {}
- function module.Part:TracePart(part: Instance, p0: Vector3, p1: Vector3, width: number, height: number)
- assert(typeof(part) == 'Instance', "Part must be an Instance")
- assert(typeof(p0) == 'Vector3', "p0 must be a Vector3")
- assert(typeof(p1) == 'Vector3', "p1 must be a Vector3")
- assert(typeof(width) == 'number', "Width must be a number")
- assert(typeof(height) == 'number', "Height must be a number")
- local Distance = (p0-p1).Magnitude
- part.CFrame = CFrame.new(p0,p1) * CFrame.new(0,0,-Distance/2)
- part.Size = Vector3.new(width,height,Distance)
- end
- module.Math = {}
- function module.Math:Lerp(a: number, b: number, t: number)
- assert(typeof(a) == 'number', "a must be a number")
- assert(typeof(b) == 'number', "b must be a number")
- assert(typeof(t) == 'number', "t must be a number")
- return a + (b - a) * t
- end
- function module.Math:Flip(num: number)
- assert(typeof(num) == 'number', "num must be a number")
- return -num
- end
- function module.Math:Largest(array: table, place: number)
- assert(typeof(array) == 'table', "array must be a table")
- assert(typeof(place) == 'number', "place must be a number")
- local otTable = {table.unpack(array)}
- table.sort(otTable, function(a, b) return a > b end)
- return otTable[place]
- end
- function module.Math:HasDecimal(num: number)
- assert(typeof(num) == 'number', "num must be a number")
- return (math.floor(num) == num)
- end
- module.Math.Epsilon = 2.2204460492503131e-16
- module.Math.Euler = math.exp(1)
- module.Math.GoldenRatio = (1 + math.sqrt(5)) / 2
- module.Instance = {}
- function module.Instance.new(class: string, parent: Instance, properties: table)
- assert(typeof(class) == 'string', "Class must be a string")
- assert(typeof(parent) == 'Instance', "Parent must be an Instance")
- assert(typeof(properties) == 'table', "Properties must be a table")
- local object = Instance.new(class, parent)
- if properties ~= nil then
- for key,value in pairs(properties or {["Name"] = "Object"}) do
- object[key] = value
- end
- end
- return object
- end
- module.Sandbox = {}
- local curSandbox = {}
- function module.Sandbox:Run(name: string, funct: any)
- assert(typeof(name) == 'string', "Name must be a string")
- assert(typeof(funct) == 'function', "Code must be function")
- local cor = coroutine.create(funct)
- if curSandbox[name] then
- error("Object with name '" .. name .. "' already exists")
- else
- coroutine.resume(cor)
- curSandbox[name] = cor
- return true
- end
- end
- function module.Sandbox:Yield(name: string)
- assert(typeof(name) == 'string', "Name must be a string")
- if not curSandbox[name] then
- error("Object with name '" .. name .. "' doesn't exist")
- else
- coroutine.yield(curSandbox[name])
- return true
- end
- end
- function module.Sandbox:Resume(name: string)
- assert(typeof(name) == 'string', "Name must be a string")
- if not curSandbox[name] then
- error("Object with name '" .. name .. "' doesn't exist")
- else
- coroutine.resume(curSandbox[name])
- return true
- end
- end
- module.Random = {}
- function module.Random:new(seed: number)
- assert(typeof(seed) == 'number', "Seed must be a number")
- local randomObj = Random.new(seed)
- local objs = {}
- function objs:Number(n: number, m: number)
- assert(typeof(n) == 'number', "n must be a number")
- assert(typeof(m) == 'number', "m must be a number")
- return randomObj:NextNumber(n, m)
- end
- function objs:Integer(n: number, m: number)
- assert(typeof(n) == 'number', "n must be a number")
- assert(typeof(m) == 'number', "m must be a number")
- return randomObj:NextInteger(n, m)
- end
- function objs:Table(table: table)
- assert(typeof(table) == 'table', "Table must be a table")
- return table[randomObj:NextInteger(1, #table)]
- end
- function objs:Boolean()
- return (randomObj:NextNumber(0, 1) > 0.5)
- end
- return objs
- end
- module.Asset = {}
- function module.Asset:Load(assetId: number)
- assert(typeof(assetId) == 'number', "AssetId must be a number")
- local InsertService = game:GetService("InsertService")
- local asset
- local success, err = pcall(function()
- asset = InsertService:LoadAsset(assetId)
- end)
- if success then
- return asset:GetChildren()[1]
- else
- warn("Failed to load asset: " .. err)
- return nil
- end
- end
- local pool = {}
- module.Pool = {}
- function module.Pool:Listen(recipient: string, channel: string, funct: any)
- assert(typeof(recipient) == 'string', "Recipient must be a string")
- assert(typeof(channel) == 'string', "Channel must be a string")
- assert(typeof(funct) == 'function', "Callback must be a function")
- if not pool[channel] then
- pool[channel] = {}
- end
- pool[channel][recipient] = funct
- return true
- end
- function module.Pool:Emit(channel: string, data: any)
- assert(typeof(channel) == 'string', "Channel must be a string")
- if not pool[channel] then
- pool[channel] = {}
- end
- for key,funct in pairs(pool[channel]) do
- if typeof(data) == "table" then
- funct(table.unpack(data))
- else
- funct(data)
- end
- end
- return true
- end
- function module.Pool:Disconnect(recipient: string, channel: string)
- assert(typeof(recipient) == 'string', "Recipient must be a string")
- assert(typeof(channel) == 'string', "Channel must be a string")
- assert(pool[channel][recipient], `No recipient named "{recipient}" is listening`)
- pool[channel][recipient] = nil
- return true
- end
- local messagingService = game:GetService("MessagingService")
- module.Cross = {}
- function module.Cross:Publish(channel: string, data: any)
- assert(typeof(channel) == 'string', "Channel must be string")
- messagingService:PublishAsync(channel, data)
- return true
- end
- function module.Cross:Listen(channel: string, funct: any)
- assert(typeof(channel) == 'string', "Channel must be string")
- assert(typeof(funct) == 'function', "Callback must be function")
- return messagingService:SubscribeAsync(channel, funct)
- end
- module.Debounce = {}
- function module.Debounce.new(delayTime: number)
- assert(typeof(delayTime) == 'number', "Delay time must be number")
- local obj = {}
- obj.Allowed = true
- function obj:Pass()
- if obj.Allowed == true then
- task.spawn(function()
- obj.Allowed = false
- wait(delayTime)
- obj.Allowed = true
- end)
- return true
- else
- return false
- end
- end
- end
- function module.Schedule(funct: any, delayTime: number)
- assert(typeof(funct) == 'function', "Callback must be function")
- assert(typeof(delayTime) == 'number', "Delay time must be number")
- local doing = true
- local obj = {}
- function obj.Do()
- if doing == true then
- task.spawn(funct)
- doing = false
- end
- end
- function obj.Cancel()
- doing = false
- end
- task.spawn(function()
- wait(delayTime)
- obj.Do()
- end)
- return obj
- end
- module.Fun = module.Fun or {}
- module.Fun.Text = {}
- function module.Fun.Please(funct: any)
- assert(typeof(funct) == 'function', "What do I do? This isn't a task, or it is no task.")
- local randomObj = Random.new()
- local doIt = (randomObj:NextNumber() > 0.5)
- if doIt then
- funct()
- return true, "Sure, why not?"
- else
- return false, "I don't feel like it."
- end
- end
- function module.Fun.Text:Zalgo(text: string)
- assert(typeof(text) == 'string', "Text must be a string")
- local accents = {"̖", "̗", "̘", "̙", "̜", "̝", "̞", "̟", "̠", "̣", "̤", "̥", "̦", "̩", "̪", "̫", "̬", "̭", "̮", "̯", "̰", "̱", "̴", "̵", "̶", "͇", "͈", "͉", "͍", "͎", "͓", "͔", "͕", "͖", "͙", "͚", "̣"}
- local result = ""
- for i = 1, #text do
- result = result .. text:sub(i, i)
- for _ = 1, math.random(1, 10) do
- result = result .. accents[math.random(#accents)]
- end
- end
- return result
- end
- module.Players = {}
- function module.Players:Hook(player: Instance)
- assert(player:IsA('Player'), "Player must be a Player instance")
- local obj = {}
- obj.Player = player
- obj.Disconnect = {
- Abrupt = function()
- player:Destroy()
- end,
- Kick = function(reason: string)
- player:Kick(reason)
- end
- }
- obj.Team = {
- Set = function(teamName: string)
- local team = game:GetService("Teams"):FindFirstChild(teamName)
- if team then
- player.Team = team
- else
- error("Team " .. teamName .. " doesn't exist")
- end
- end
- }
- local leaderstats = player:FindFirstChild('leaderstats') or Instance.new('Folder')
- leaderstats.Name = 'leaderstats'
- leaderstats.Parent = player
- obj.Leaderstats = setmetatable({}, {
- __index = function(_, key)
- return leaderstats:FindFirstChild(key) and leaderstats[key].Value or nil
- end,
- __newindex = function(_, key, value)
- assert(typeof(value) == 'number' or typeof(value) == 'string', "Value must be a number or a string")
- local statObject = leaderstats:FindFirstChild(key)
- if not statObject then
- statObject = typeof(value) == 'number' and Instance.new('NumberValue') or Instance.new('StringValue')
- statObject.Name = key
- statObject.Parent = leaderstats
- end
- statObject.Value = value
- end
- })
- return obj
- end
- function module.Players:AddFunction(callback: any)
- local obj = {}
- local connections = {}
- task.spawn(function()
- for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
- callback(player)
- end
- end)
- task.spawn(function()
- local connection = game:GetService("Players").PlayerAdded:Connect(function(player)
- callback(player)
- end)
- table.insert(connections, connection)
- end)
- function obj:Disconnect()
- for _, connection in ipairs(connections) do
- connection:Disconnect()
- end
- end
- return obj
- end
- module.Throttling = {}
- function module.Throttling.new(timeFrame: number, maxCalls: number)
- assert(typeof(timeFrame) == 'number', "Time frame must be a number")
- assert(typeof(maxCalls) == 'number', "Max calls must be a number")
- local obj = {}
- obj.TimeFrame = timeFrame
- obj.MaxCalls = maxCalls
- obj.CallCount = 0
- obj.LastCallTime = 0
- function obj:Pass()
- local currentTime = tick()
- if self.LastCallTime == 0 or (currentTime - self.LastCallTime) > self.TimeFrame then
- -- Reset call count if time frame has passed since last call
- self.CallCount = 0
- end
- if self.CallCount < self.MaxCalls then
- -- Call count is below the limit, allow the call
- self.CallCount = self.CallCount + 1
- self.LastCallTime = currentTime
- return true
- else
- -- Call count has reached the limit, don't allow the call
- return false
- end
- end
- return obj
- end
- module.Timer = {}
- function module.Timer.new(time: number, tickInterval: number)
- assert(typeof(time) == 'number', "Time must be a number")
- assert(typeof(tickInterval) == 'number', "Tick interval must be a number")
- local obj = {}
- obj.TimeLeft = time
- obj.TickInterval = tickInterval
- obj.Running = false
- obj.Tick = function() end -- Default to empty function
- obj.Ended = function() end -- Default to empty function
- function obj:Start()
- assert(not self.Running, "Timer is already running")
- self.Running = true
- task.spawn(function()
- while self.Running and self.TimeLeft > 0 do
- self.Tick(self.TimeLeft)
- wait(self.TickInterval)
- self.TimeLeft = self.TimeLeft - self.TickInterval
- end
- if self.Running then -- Only call Ended if the timer wasn't cancelled
- self.Ended()
- end
- end)
- end
- function obj:Cancel()
- self.Running = false
- end
- return obj
- end
- module.Array = {}
- function module.Array:Limit(array: table, lowerBound: number, upperBound: number)
- assert(type(array) == 'table', "Array must be a table")
- assert(typeof(lowerBound) == 'number', "Lower bound must be a number")
- assert(typeof(upperBound) == 'number', "Upper bound must be a number")
- local result = {}
- for i = 1, #array do
- if array[i] >= lowerBound and array[i] <= upperBound then
- table.insert(result, array[i])
- end
- end
- return result
- end
- function module.Array:ForEach(array: table, callback: any)
- assert(type(array) == 'table', "Array must be a table")
- assert(typeof(callback) == 'function', "Callback must be a function")
- for index, value in ipairs(array) do
- callback(value)
- end
- return true
- end
- local base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
- module.Misc = {}
- module.Misc.Base64 = {}
- function module.Misc.Base64:Encode(data)
- data = tostring(data)
- return ((data:gsub('.', function(x)
- local r, b = '', x:byte()
- for i = 8, 1, -1 do r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0') end
- return r;
- end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
- if (#x < 6) then return '' end
- local c = 0
- for i = 1, 6 do c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0) end
- return base64Chars:sub(c + 1, c + 1)
- end)..({ '', '==', '=' })[#data % 3 + 1])
- end
- function module.Misc.Base64:Decode(data)
- data = tostring(data)
- data = data:gsub('[^'..base64Chars..'=]', '')
- return (data:gsub('.', function(x)
- if (x == '=') then return '' end
- local r, f = '', (base64Chars:find(x) - 1)
- for i = 6, 1, -1 do r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and '1' or '0') end
- return r;
- end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
- if (#x ~= 8) then return '' end
- local c = 0
- for i = 1, 8 do c = c + (x:sub(i, i) == '1' and 2 ^ (8 - i) or 0) end
- return string.char(c)
- end))
- end
- local DataStoreService = game:GetService("DataStoreService")
- module.DataStore = {}
- function module.DataStore:GetStore(storeName: string)
- assert(typeof(storeName) == 'string', "Store name must be a string")
- local dataStore = DataStoreService:GetDataStore(storeName)
- local store = {}
- function store:Get(key: string, value: any)
- local success, result = pcall(function()
- return dataStore:GetAsync(key)
- end)
- if success then
- return result
- else
- warn("Failed to get data for key: " .. key)
- end
- end
- function store:Set(key: string, value: any)
- local success, result = pcall(function()
- dataStore:SetAsync(key, value)
- end)
- if success then
- return true
- else
- warn("Failed to set data for key: " .. key)
- return false
- end
- end
- return store
- end
- local function tryRequire(moduleScript)
- local status, result = pcall(require, moduleScript)
- if status then
- return result
- else
- print(string.format("Failed to require %s: %s", moduleScript:GetFullName(), tostring(result)))
- return nil
- end
- end
- local loadstringModule = tryRequire(script.Loadstring)
- function module.Loadstring(str: string, env: table)
- assert(typeof(str) == 'string', "Code must be string")
- if env then
- assert(typeof(env) == 'table', "Environment must be table")
- end
- return loadstringModule(str, env)
- end
- return module
Add Comment
Please, Sign In to add comment