Advertisement
Quoteory

Tool Module

Nov 14th, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. local ToolModule = {}
  2.  
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4.  
  5. local modules = script.Parent.Parent.Modules
  6. local sharedModules = ReplicatedStorage.SharedModules
  7. local DataManager = require(modules.DataManager)
  8. local ItemDatabase = require(modules.ItemDatabase)
  9.  
  10.  
  11. local function FindCharacterTool(character, toolName)
  12.     return character:FindFirstChild(toolName)  
  13. end
  14.  
  15. local function FindBackpackTool(backpack, toolName)
  16.     return backpack:FindFirstChild(toolName)
  17. end
  18.  
  19. local function AddToBackpack(player, toolName)
  20.     local tool = ItemDatabase:GetTool(toolName)
  21.     local character = player.Character
  22.     local backpack = player:FindFirstChild("Backpack")
  23.     local existingTool
  24.     local toolClone
  25.    
  26.     if not tool or not backpack or not character then return end
  27.  
  28.     existingTool = FindBackpackTool(backpack, toolName) or FindCharacterTool(character, toolName)
  29.    
  30.     if existingTool then return end
  31.    
  32.     toolClone = tool:Clone()
  33.     toolClone.Parent = backpack
  34. end
  35.  
  36. local function RemoveFromBackpack(player, toolName)
  37.     local backpack =  player:FindFirstChild("Backpack")
  38.     local character = player.Character
  39.     local backpackTool
  40.     local characterTool
  41.    
  42.     if not backpack or not character then return end
  43.    
  44.     backpackTool = FindBackpackTool(backpack, toolName)
  45.     characterTool = FindCharacterTool(character, toolName)
  46.        
  47.     if backpackTool then backpackTool:Destroy() end
  48.     if characterTool then characterTool:Destroy() end
  49. end
  50.  
  51. function ToolModule:GetToolInventory(player)
  52.     local data = DataManager:GetPlayerData(player)
  53.     if data then return data.Tools end     
  54. end
  55.  
  56. function ToolModule:AddTool(player, toolName)
  57.     local toolInventory = self:GetToolInventory(player)
  58.  
  59.     if not toolInventory or toolInventory[toolName] then return end
  60.    
  61.     toolInventory[toolName] = true
  62.     AddToBackpack(player, toolName)
  63. end
  64.  
  65. function ToolModule:RemoveTool(player, toolName)
  66.     local toolInventory = self:GetToolInventory(player)
  67.    
  68.     if not toolInventory or not toolInventory[toolName] then return end
  69.    
  70.     toolInventory[toolName] = nil
  71.     RemoveFromBackpack(player, toolName)
  72. end
  73.  
  74. function ToolModule:AddAllToolsToBackpack(player)
  75.     local toolInventory = self:GetToolInventory(player)
  76.    
  77.     if not toolInventory then return end
  78.  
  79.     for toolName, _ in pairs(toolInventory) do
  80.         AddToBackpack(player, toolName)
  81.     end
  82. end
  83.  
  84. return ToolModule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement