Changing module values issue

Hello. I’m making a gun system and I want to add “perks/traits” that players can equip. For example: “Reload speed for all your guns is increased by 20%,” but the issue with this is that I’m not sure how I would implement this into my current code? So far, I have a module in each tool that stores all the data for the tool that the server-script for the gun uses. Would I just be able to modify this data by doing something like ``RELOAD_TIME = 2.2"? I’m not sure how I would be able to implement this.

local Constants = {
	FIRE_MODE = "Semi", -- Only accepts "Semi" or "Auto" as arguments
	SHOT_COOLDOWN = .075,
	BULLET_LENGTH = 125,
	BULLETS_PER_SHOT = 1,
	BULLET_DAMAGE = 12,
	RELOAD_TIME = 2.5,
	MAGAZINE_SIZE = 15,
	RAY_SPREAD = 2,
}

return Constants

Well maybe have like a dictionary that contains all perks where it goes like this:

local perks = {
RELOAD_TIME = 20
}

You then loop through all the perks in that table and add/multiply/whatever to this:

And yeah thats the basic way of doing it

1 Like

So I can basically just change the values inside the module by doing something like?

local GunModule = require(gun.Constants)

GunModule.RELOAD_TIME *= 0.9 -- Would this work???

Yup! Except if you require the module from another script (depending on what type of script you are using) it may still be the default, just so ya know

1 Like

Well the way my system works is I’m just requiring it through a server script that is inside the gun’s Tool object. Heres a little snippet of code

local function onShootEvent(player: Player, mousePosition: Vector3)
	if ammoValue.Value <= 0 then -- If the player tries to shoot but has no ammo, then the gun will reload
		onReloadEvent()
	end
	if not canShoot() then
		return
	end

	ammoValue.Value -= 1

	sounds.Fire:Play()

	isCoolingDown = true
	task.delay(Constants.SHOT_COOLDOWN, function() -- Here I use data from the module
		isCoolingDown = false
	end)

	for amountOfBullets = 1, Constants.BULLETS_PER_SHOT do -- Here I also use data from the module
		fireBullet(mousePosition)
	end
end

Well in that case you should be fine!

1 Like

Ok, I’ll experiment with it and see if it works, thanks for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.