Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Package Module
- -- NodeSupport
- -- January 19, 2019
- --[[
- Package Class, will produce packages with a call function [ PackageService.new() ], Packages can be modified
- and have custom metamethods allowing for maximum manipulation from a 3rd party script.
- Class Functions / Instance Functions
- :Destroy() - Destroys the object and cleans it from the table
- :Claim() - Claims the object and puts it in the users storage
- :Deliver() - Confirms the deliver and rewards user
- Index / Settings
- .Level - Set and view the level in which a package is (1 = envelope, 2 = package)
- .Ownership - A player who controls and owns the package (only they can deliver this package)
- .ID - the ID in a GUID form (STRING)
- Service
- .New(Player) - Creates a Package
- .Delete(Player) - Deletes the players table for cleanup
- Example
- local Package = PackageService.New(Player)
- spawn(function()
- print(Package.Level)
- wait(5)
- Package:Destroy()
- end)
- --]]
- local PackageModule = {}
- local Packages = {}
- local PackageClass = {_ratio = {1, 1, 1, 2}}
- local CreatePackage, ObjectManager, PlayerData
- local HttpService = game:GetService("HttpService")
- function PackageClass:Deliver()
- end
- function PackageClass:Claim()
- local Player = self.Ownership
- local PlayerStorage = PlayerData[Player].Storage
- if PlayerStorage.UsedStorage + 1 >= PlayerStorage.MaxStorage then
- PlayerStorage.UsedStorage = PlayerStorage.MaxStorage
- else
- PlayerStorage.UsedStorage = PlayerStorage.UsedStorage + self.Level
- end
- end
- function PackageClass:Destroy() -- Destroy a package
- if Packages[self.Ownership] then
- for index,packageData in pairs(Packages[self.Ownership]) do -- Cycle through all packages checking for proper ID
- if self.ID == packageData.ID then
- self.Object:Destroy()
- table.remove(Packages[self.Ownership], index) -- Removing package
- end
- end
- end
- end
- function PackageModule.New(Player) -- Creating a Custom Instance using the Metamethod of __index = PackageClass
- local ID, Level = tostring(HttpService:GenerateGUID(false)), PackageClass._ratio[math.random(1, #PackageClass._ratio)] -- Id, Level
- local Package = { -- Game Framework requires Object Services to be within a metatable
- Object = ObjectManager.New({
- Name = ID,
- Type = "Package", -- Type is Package, special settings for it
- Player = Player,
- Level = Level,
- }),
- Level = Level, -- May be deprecated! Random level selection of the package
- ID = ID,
- Ownership = Player,
- };
- setmetatable(Package, {
- __index = PackageClass -- Custom Class/Instance now!
- });
- Packages[Player] = Packages[Player] or {}
- table.insert(Packages[Player], Package) -- Inserting the Package
- return Package
- end
- function PackageModule.Delete(Player) -- Cleanup / Preventing Memory leaks for when a user leaves
- local PlayerPackages = Packages[Player]
- if PlayerPackages then
- PlayerPackages = nil -- Deleting table
- end
- end
- function PackageModule:Start()
- ObjectManager = self.Modules.ObjectManager
- PlayerData = self.Modules.PlayerData
- end
- return PackageModule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement