The ToolHandling System is made up of
- ToolClass
- ToolHandler
- 1 Data, 1 Module for each Tool added (Rn there is one tool)
I would appreciate any feedback and advice on this. This is my 2nd time using a Class Module.
I genuinely would just like to improve this system and learn the use-case for class modules.
TOOLHANDLER:
local module = {}
--SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
--VARIABLES
local ModuleLoader = require(Players.LocalPlayer.PlayerScripts.Client.ModuleLoader)
local ToolClass = require(Players.LocalPlayer.PlayerScripts.Client.Modules.Classes.ToolClass)
--PRIVATE VARIABLES
local LocalPlayer = Players.LocalPlayer
local RegisteredTools = {}
--PRIVATE FUNCTIONS
local function RegisterTool(Tool: Tool, Humanoid: Humanoid)
for _, MetaTable in pairs(RegisteredTools) do
if MetaTable.Tool == Tool then
return
end
end
local ToolObject = ToolClass.New(Tool)
table.insert(RegisteredTools, ToolObject)
ToolObject.Tool.Equipped:Connect(function() ToolObject:Equipped(Humanoid) end)
ToolObject.Tool.Unequipped:Connect(function() ToolObject:Unequipped(Humanoid) end)
end
--PUBLIC FUNCTIONS
function module.Start()
LocalPlayer.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
for _, MetaTable in pairs(RegisteredTools) do
MetaTable:Destroy()
end
RegisteredTools = {}
Humanoid.Died:Connect(function()
for _, MetaTable in pairs(RegisteredTools) do
MetaTable:Unequipped(Humanoid)
end
end)
for _, Tool in pairs(LocalPlayer.Backpack:GetChildren()) do
if Tool:IsA("Tool") and Humanoid.Health ~= 0 then
RegisterTool(Tool, Humanoid)
end
end
LocalPlayer.Backpack.ChildAdded:Connect(function(Tool)
if Tool:IsA("Tool") and Humanoid.Health ~= 0 then
RegisterTool(Tool, Humanoid)
end
end)
end)
end
return module
TOOLCLASS
local module = {}
module.__index = module
--SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
--VARIABLES
local ModuleLoader = require(Players.LocalPlayer.PlayerScripts.Client.ModuleLoader)
--PRIVATE VARIABLES
local LocalPlayer = Players.LocalPlayer
local DataModules = {}
--PRIVATE FUNCTIONS
--PUBLIC FUNCTIONS
function module.Start()
for _, DataModule in ipairs(script.Parent.Parent.Handlers.ToolHandler.ToolData:GetChildren()) do
if DataModule:IsA("ModuleScript") then
DataModules[DataModule.Name] = require(DataModule)
end
end
end
function module.New(Tool: Tool)
for DataName, DataModule in pairs(DataModules) do
if DataName == Tool.Name then
local self = setmetatable({}, module)
self.Tool = Tool
self.ToolModule = DataModule.ToolModule
self.ToolAnimationInfo = DataModule.ToolAnimationInfo
return self
end
end
end
function module:Equipped(Humanoid: Humanoid)
if self.Tool and Humanoid.Health ~= 0 then
self.ToolModule.EquippedFunction(Humanoid)
end
end
function module:Unequipped(Humanoid: Humanoid)
self.ToolModule.UnequippedFunction(Humanoid)
end
function module:Destroy()
setmetatable(self, nil)
table.clear(self)
end
return module
Example: ToolData Module / ToolFunction Module
return {
ToolName = "ID Card",
ToolModule = require(script.Parent.Parent.ToolFunctions["ID Card"]),
ToolAnimationInfo = {}
}
local module = {}
--SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
-- VARIABLES
local ModuleLoader = require(Players.LocalPlayer.PlayerScripts.Client.ModuleLoader)
local IDCardEvent = ReplicatedStorage.Network.ToolEvents.IDCard
--PRIVATE VARIABLES
local LocalPlayer = Players.LocalPlayer
local Equipped = false
--PRIVATE FUNCTIONS
--PUBLIC FUNCTIONS
function module.EquippedFunction(Humanoid)
Equipped = true
IDCardEvent:FireServer(Equipped)
end
function module.UnequippedFunction(Humanoid)
if Equipped == true then
Equipped = false
end
end
return module