Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. -- Package Module
  2. -- NodeSupport
  3. -- January 19, 2019
  4.  
  5.  
  6. --[[
  7.    
  8.     Package Class, will produce packages with a call function [ PackageService.new() ], Packages can be modified
  9.     and have custom metamethods allowing for maximum manipulation from a 3rd party script.
  10.    
  11.     Class Functions / Instance Functions
  12.    
  13.         :Destroy() - Destroys the object and cleans it from the table
  14.         :Claim() - Claims the object and puts it in the users storage
  15.         :Deliver() - Confirms the deliver and rewards user
  16.    
  17.     Index / Settings
  18.    
  19.         .Level - Set and view the level in which a package is (1 = envelope, 2 = package)
  20.         .Ownership - A player who controls and owns the package (only they can deliver this package)
  21.         .ID - the ID in a GUID form (STRING)
  22.    
  23.     Service
  24.    
  25.         .New(Player) - Creates a Package
  26.         .Delete(Player) - Deletes the players table for cleanup
  27.        
  28.     Example
  29.    
  30.         local Package = PackageService.New(Player)
  31.         spawn(function()
  32.             print(Package.Level)
  33.             wait(5)
  34.             Package:Destroy()
  35.         end)
  36. --]]
  37.  
  38. local PackageModule = {}
  39. local Packages = {}
  40. local PackageClass = {_ratio = {1, 1, 1, 2}}
  41. local CreatePackage, ObjectManager, PlayerData
  42. local HttpService = game:GetService("HttpService")
  43.  
  44. function PackageClass:Deliver()
  45.    
  46. end
  47.  
  48. function PackageClass:Claim()
  49.     local Player = self.Ownership
  50.     local PlayerStorage = PlayerData[Player].Storage
  51.     if PlayerStorage.UsedStorage + 1 >= PlayerStorage.MaxStorage then
  52.         PlayerStorage.UsedStorage = PlayerStorage.MaxStorage
  53.     else
  54.         PlayerStorage.UsedStorage = PlayerStorage.UsedStorage + self.Level
  55.     end
  56. end
  57.  
  58. function PackageClass:Destroy() -- Destroy a package
  59.     if Packages[self.Ownership] then
  60.         for index,packageData in pairs(Packages[self.Ownership]) do -- Cycle through all packages checking for proper ID
  61.             if self.ID == packageData.ID then
  62.                 self.Object:Destroy()
  63.                 table.remove(Packages[self.Ownership], index) -- Removing package
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. function PackageModule.New(Player) -- Creating a Custom Instance using the Metamethod of __index = PackageClass
  70.     local ID, Level = tostring(HttpService:GenerateGUID(false)), PackageClass._ratio[math.random(1, #PackageClass._ratio)] -- Id, Level
  71.     local Package = { -- Game Framework requires Object Services to be within a metatable
  72.         Object = ObjectManager.New({
  73.             Name = ID,
  74.             Type = "Package", -- Type is Package, special settings for it
  75.             Player = Player,
  76.             Level = Level,
  77.         }),
  78.         Level = Level, -- May be deprecated! Random level selection of the package
  79.         ID = ID,
  80.         Ownership = Player,
  81.     };
  82.     setmetatable(Package, {
  83.         __index = PackageClass -- Custom Class/Instance now!
  84.     });
  85.     Packages[Player] = Packages[Player] or {}
  86.     table.insert(Packages[Player], Package) -- Inserting the Package
  87.     return Package
  88. end
  89.  
  90. function PackageModule.Delete(Player) -- Cleanup / Preventing Memory leaks for when a user leaves
  91.     local PlayerPackages = Packages[Player]
  92.     if PlayerPackages then
  93.         PlayerPackages = nil -- Deleting table
  94.     end
  95. end
  96.  
  97. function PackageModule:Start()
  98.     ObjectManager = self.Modules.ObjectManager
  99.     PlayerData = self.Modules.PlayerData
  100. end
  101.  
  102. return PackageModule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement