Hello, I am currently making a combat module for a project I am working on with a team (I am the primary / sole programmer.) I recently finished an early portion of the module which includes basic M1 damage, cooldowns and status effects. I was wondering on how I could improve the module in terms of:
-Easiness to change and modify the module if necessary.
-Readability.
-Reduction of memory leaks / optimization of code
-Any other possible improvements.
Heres the scripts involved:
Combat module:
local module = {}
local statuses = require(game.ReplicatedStorage.StatusModule)
EntityStatusTable = {}
local function TableCount(t)
local c = 0
for i, v in pairs(t) do c += 1 end
return c
end
function module.M1(Player, ToolObject, WeaponHitbox, Damage, SwingCooldown, Duration, Animation, StatusTable)--duration should be less than (or equal to) the swing cooldown
local Cooldown = false
local PlayerDebounces = {}
if not Cooldown then
Cooldown = true
local Dmg = WeaponHitbox.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if human and not PlayerDebounces[human.Parent] then
human.Health -= Damage
PlayerDebounces[human.Parent] = true
if not EntityStatusTable[human.Parent] then
EntityStatusTable[human.Parent] = {}
end
for i, v in pairs(StatusTable) do
if not table.find(EntityStatusTable[human.Parent], v.Status) then
table.insert(EntityStatusTable[human.Parent], v.Status)
task.defer(function()
v.Status(human.Parent, v.ModTier)
task.wait(statuses.DurationMult[v.Status]+v.ModTier)
table.remove(EntityStatusTable[human.Parent], table.find(EntityStatusTable[human.Parent], v.Status))
end)
end
end
task.wait(Duration)
PlayerDebounces[human.Parent] = nil
end
end)
task.wait(Duration)
Dmg:Disconnect()
task.wait(SwingCooldown-Duration)
Cooldown = false
end
end
function module.WeaponSkill(Player, Damage, Cooldown, Speed)
end
function module.RangedWeaponAttack()
end
function module.MouseHitTransfer()--Sends a location from the mouse to the server with a remote event
end
function module.AttackDamage()
end
return module
Note that some functions are empty, I might planning to use them in future use.
Status Module
local StatusEffects = {}
StatusEffects.DurationMult = {}
StatusEffects.Poison = function(Target, Tier)
local damageTicks = 0
while true do
Target.Humanoid.Health -= Tier*5
damageTicks += 1
if damageTicks >= Tier*4 then
break
end
task.wait(1)
end
end
StatusEffects.Freeze = function(Target, Tier)
if Target.Torso.Anchored == false then
Target.Torso.Anchored = true
task.wait(1+Tier-math.sqrt(Tier))
Target.Torso.Anchored = false
end
end
StatusEffects.DurationMult[StatusEffects.Poison] = 4
StatusEffects.DurationMult[StatusEffects.Freeze] = 5
return StatusEffects
Example of a script which might call the modules:
tool = script.Parent
module = require(game.ReplicatedStorage.MeleeModule)
statusEffects = require(game.ReplicatedStorage.StatusModule)
tool.Activated:Connect(function()
local person = script.Parent
module.M1(script.Parent.Parent, script.Parent, script.Parent.Hitbox, 10, 0.75, 0.5, nil, {{Status = statusEffects.Poison, ModTier = 2}, {Status = statusEffects.Freeze, ModTier = 2}} )
end)