Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ToolModule = {}
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local modules = script.Parent.Parent.Modules
- local sharedModules = ReplicatedStorage.SharedModules
- local DataManager = require(modules.DataManager)
- local ItemDatabase = require(modules.ItemDatabase)
- local function FindCharacterTool(character, toolName)
- return character:FindFirstChild(toolName)
- end
- local function FindBackpackTool(backpack, toolName)
- return backpack:FindFirstChild(toolName)
- end
- local function AddToBackpack(player, toolName)
- local tool = ItemDatabase:GetTool(toolName)
- local character = player.Character
- local backpack = player:FindFirstChild("Backpack")
- local existingTool
- local toolClone
- if not tool or not backpack or not character then return end
- existingTool = FindBackpackTool(backpack, toolName) or FindCharacterTool(character, toolName)
- if existingTool then return end
- toolClone = tool:Clone()
- toolClone.Parent = backpack
- end
- local function RemoveFromBackpack(player, toolName)
- local backpack = player:FindFirstChild("Backpack")
- local character = player.Character
- local backpackTool
- local characterTool
- if not backpack or not character then return end
- backpackTool = FindBackpackTool(backpack, toolName)
- characterTool = FindCharacterTool(character, toolName)
- if backpackTool then backpackTool:Destroy() end
- if characterTool then characterTool:Destroy() end
- end
- function ToolModule:GetToolInventory(player)
- local data = DataManager:GetPlayerData(player)
- if data then return data.Tools end
- end
- function ToolModule:AddTool(player, toolName)
- local toolInventory = self:GetToolInventory(player)
- if not toolInventory or toolInventory[toolName] then return end
- toolInventory[toolName] = true
- AddToBackpack(player, toolName)
- end
- function ToolModule:RemoveTool(player, toolName)
- local toolInventory = self:GetToolInventory(player)
- if not toolInventory or not toolInventory[toolName] then return end
- toolInventory[toolName] = nil
- RemoveFromBackpack(player, toolName)
- end
- function ToolModule:AddAllToolsToBackpack(player)
- local toolInventory = self:GetToolInventory(player)
- if not toolInventory then return end
- for toolName, _ in pairs(toolInventory) do
- AddToBackpack(player, toolName)
- end
- end
- return ToolModule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement