Organizing Code

So basically, I wajnt to organize and optimize my code for a power system im making. Basically its combat but t heirs diff characters you can choose, what do you guys suggest to organize the code b/c rn i have a “heavy” module script that basically is fired from the server wehnever you click m2 , and based on your character you do a different heavy ( the module script finds the heavy based on your char and then does the function. But rn i have only 2 characters and its already like 600 lines of code and i want to clean the code up so i can easily find the code of a character cause rn 600 lines of code means an annoying scroll evrytime i want to tweak something… thanks and would appreciate the help asap. Btw each heahvy does this :can do attack, varaibles, hitbox ( run service + region 3 ) , checking victim health, adding the “attacked tag” , checking blocking : if blocking than _, otherwise _ , checking if its an arial attack for dmg boost / diff vfx /knockback etc… anyways im not gonna continue but u get the point, theirs a lot…

Well without sharing the code I can’t really help much but from what I gathered from your post I suggest that You apply the DRY principle (Don’t repeat yourself)

from your post it seems like you are doing the check with every heavy attack that means you would have duplicate code which would get overwhelming when you add more heavy attacks or modify how they work. You should instead wrap common check on a function so you can easily apply them on all heavy attacks and easily modify them

an example:

Don’t:


local CanBlock = false

local function HeavyAttack1()
    if not CanBlock then
        print(Attacked)
    end
end

local function HeavyAttack2()
    if not CanBlock then
        print(Attacked)
    end
end

Do:

local CanBlock = false

local function BlockCheck()
    if not CanBlock then

        return true
    end
end

local function HeavyAttack1()
       if BlockCheck() then
        print("Attacked")
       end
end

local function HeavyAttack2()
       if BlockCheck() then
        print("Attacked")
       end
end

lmk if you have more questions :smiley:

don’t both things just run the same thing

You are right but it can be easily changed later on instead of having to retype the check on every function which is the main point of the DRY principle I talked about

What you can try to do is make a module for each different character and then call those same functions. For example, lets say a user presses m1 or m2 to do a heavy attack, instead of checking what character the user has in one script, make multiple module scripts and then call a specific function from the module script of the power that the user has.

local power = "Some Power"

UserInputService.InputBegan:Connect(function(key, gpe)
    if (check the key you want) then
        game.ServerScriptService[power].HeavyPunch() --this should call a different heavy punch based on the power of the user
    end
end)

You can also make a module script for stunning and/or combat and such where you can check the players health and such.

i have a module for stunning, im calling it each time i do an attack, and heres my s ystem rn
client clicks m1, fire server, server calls module.choose, module.choose chooses and does the function based on the players character ( so module.choose checks the char and does everthing after tat )

so i check if the victim is blockikngh but i can aply this, for all the little things like creating hitbox’s i can do it in a funtion.

1 Like