Can I send values to module scripts?

Greetings,

I am new to module scripts, in fact i never used them before. I am making a pvp game that has abilities that reduce dmg taken and those that boost your dmg output. So in order not to have to copy paste ugly complicated if then statements i thought i’d make a module script and just require it each time

when dmg occurs there are 4 possibilites

attacker III victim
no dmg boost III no dmg reduction
dmg boost III no dmg reduction
no dmg boost III dmg reduction
dmg boost III dmg reduction

as you can imagine this would involve a big tangle of if statements so i thought i’d make 1 module script serve as the calculator

however to calculate it needs 3 variables
the actual dmg of the attack
does the attacker have attack boost?
does the victim have dmg reduction?

however i’ve not found anywhere a way to communicate these 3 variables to the module

is it even possible? is there a better way?

thank you for your time

You can pass in those values through the function like you usually would.
Here are some examples:
Module

function ModuleName.DamageCheck(damage, attackerBoost, victimDamageReduction)
	-- code goes here
end

In another script

ModuleName.DamageCheck(5, true, false)

However, seeing as you described it as “a big tangle of if statements”, do you mean that you’re checking for all 4 possibilities in seperate if/elseif statements or are you using 2 if statements to check for damage/reduction and then apply the effects that way? If you’re doing the 4 possibilities option, it’d be better to do the latter instead, which would look like this

function ModuleName.DamageCheck(damage, attackerBoost, victimDamageReduction)
	if attackerBoost then
		damage *= 2
	end
	if victimDamageReduction then
		damage /= 2
	end
	return damage
end

edit Even better, you can have the ModuleName handle damaging the player too, which would look similar to this

function ModuleName.HitPlayer(hitPlayer, damage, attackerBoost, hitDamageReduction)
	local HitHumanoid = hitPlayer:FindFirstChildOfClass("Humanoid")
	if HitHumanoid then
		if attackerBoost then
			damage *= 2
		end
		if hitDamageReduction then
			damage /= 2
		end
		HitHumanoid:TakeDamage(damage)
	end
	return false
end

The way this is written means that you also don’t need to include the last 2 arguments when calling it; meaning ModuleName.HitPlayer(player, 4) is valid!

local module = {}

function module.calculateAB(dmg,abLevel)
    if abLevel == 1 then
        local boost = math.random(1,10)
        local result = dmg + boost
        return result
    elseif abLevel == 2 then
        local boost = math.random(10,20)
        local result = dmg + boost
        return result
    elseif abLevel == 3 then
        local boost = math.random(20,30)
        local result = dmg + boost
        return result
   end
end

function module.calculateDR(dmg,drLevel)
    if drLevel == 1 then
        local reduce = 1.25
        local result = dmg / reduce
        return result
    elseif drLevel == 2 then
        local reduce = 1.5
        local result = dmg / reduce
        return result
    elseif drLevel == 3 then
        local reduce = 2
        local result = dmg / reduce
        return result
   end
end

function module.Damage(plr,dmg,ab,dr)
    local boost
    local reduce
    if not ab and not dr then
        local result = dmg
        return result
    elseif ab and not dr then
        boost = module.calculateAB(dmg,plr.abLevel.Value)
        return boost
    elseif dr and not ab then
        reduce = module.calculateDR(dmg,plr.drLevel.Value)
        return reduce
    elseif dr and ab then
        boost = module.calculateAB(dmg,plr.abLevel.Value)
        reduce = module.calculateDR(boost,plr.drLevel.Value)
        return reduce
    end
end

return module

I made this with Datastores and skill leveling in mind. Instead of just taking the three variables, the main damage calculator takes a player argument as well (assuming it’ll be used with a RemoteEvent). plr.abLevel and plr.drLevel refer to NumberValues. They should be loaded onto the player using Datastores, as your game likely would. These values allow you to change the levels of both the attack skill and damage reduction skill.

Now, looking at the code I showed you, you can see there are a lot of if statements. With the amount of variables that’s required to calculate damage in the way you envision it, there will be a lot of if statements. There’s no way around it. Just know you made the right decision when you chose to use a module. Good luck!

i don’t see in your code, how does the module know if what is the level of the ability? like i said my main problem is that the module can’t know if the attacker has dmg boost or not when calculating it

ok i see from the post LexiDog5 made that i can indeed send a number of variables when requiring the module, allowing for what i need, i think

The values that are loaded into the player are loaded in by a Datastore. If you want your players to save their stats, you’re going to have to learn about them.

Also, if you’re looking to make type advantages-- you’re going to have to use a function that checks player types and sets attack boost/damage reduction accordingly.
(which means ur gonna have to pass another variable)

Thank you, this is exactly what i was hoping for

one thing that is confusing me is why are (attackerBoost) and (VictimDamageReduction) in brackets () in the if statements? is that the necessary syntax or something else?

edit: how do i reference the hit player in the server script? so that it recognizes it as HitPlayer in the module?

You would pass in a player’s character as the first parameter (aka their model that they control located in Workspace, their player.Character.) If you’re detecting hits via the touched event, you can get the hit player’s character by checking the parent of the part that you hit. Make sure you check if there’s a humanoid in the parent of the hit part using FindFirstChildOfClass, or else you’ll get a lot of errors.

I see, thank you very much for your help, i’ll play around with this and see if i can make it work