Review on an early combat module

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)

I like how this module is starting. Some areas for improvement are the following IMO:

  • Each of your m1’s have way too many parameters, try simplifying it and holding the rest in a dictionary.

  • Although equations are always solid to have, for effects you should probably just have them as values. Let’s say you need to tweak number values after to nerf/buff a skill. If you make it equation based you need to put arbitrary numbers to achieve what you want.

  • Try to make a module to keep your hitboxes separate and just call a function for it

Simply put clean the code up, its easier to debug and use separate data tables, passing only 1 parameter to access those features in a table

1 Like

In terms of readability, you should add indents, lines for spacing, and most importantly, comments. Especially if you work with a team. Even in my own code that I made, I forget what something does and a comment lets me easily remember. Also, for functions that require large amounts of data, I reccomend using a dictionary to keep the arguments easy to read

For the overall structure, you’re going to want a module for each character’s health, each weapon, and one for damage functions/hitboxes. I’m just going to dump my Health Module and Hitboxes guide:

Hitboxes
Health Modules

1 Like