How to "boost" values from a module?

I’m working on a gun system and I have a ModuleScript for each gun in the game that contains it’s information. I was wondering if there is any way that I would be able to “multiply” these values and make it affect the other scripts that are using these values.

local Data = {
	BULLET_DAMAGE = 10,
	BULLET_SPREAD = 2,
	BULLET_TRAVEL_DISTANCE = 150,
	BULLETS_PER_ROUND = 1,
	BURST_DELAY = 0.05,
	BURST_SIZE = 3,
	FIRE_MODE = "Semi-Automatic", -- Only accepts "Semi-Automatic", "Automatic", or "Burst."
	MAGAZINE_CAPACITY = 12,
	RATE_OF_FIRE = 0.08,
	RECOIL = 0.02,
	RELOAD_TIME = 2.5
}

return Data

If you are a confused, I was hoping I could be able to do something like this:

local FIRE_RATE_BOOST = 1.5

pistolData.RATE_OF_FIRE *= FIRE_RATE_BOOST

Unfortunately, I’m not sure if this would work, due to how ModuleScripts work. Is there any way to achieve what I’m trying to do?

2 Likes

Maybe set your values like so:

local Data = {}

Data.BULLET_DAMAGE = 10
Data.BULLET_SPREAD = 2
Data.BULLET_TRAVEL_DISTANCE = 150
Data.BULLETS_PER_ROUND = 1
Data.BURST_DELAY = 0.05
Data.BURST_SIZE = 3
Data.FIRE_MODE = "Semi-Automatic" -- Only accepts "Semi-Automatic", "Automatic", or "Burst."
Data.MAGAZINE_CAPACITY = 12
Data.RATE_OF_FIRE = 0.08
Data.RECOIL = 0.02
Data.RELOAD_TIME = 2.5

return Data

It did work for me.

2 Likes

You’ve just expanded the Module.

What’s your point here then?

1 Like

Basically, I’m trying to make sort of “traits” that players can equip which will boost the stats from their guns. For example: “Your reload speed is increased by 10%” (Just to clarify)

1 Like

I see, so why not try using attributes for such a purpose? And why even try multiplying a module scripts values?

2 Likes

The best and most basic Idea would be to just require the module in every script. it would be much more efficient to just compile “global code/storage” into a module.

Although, These values need to be edited manually, which can be a really big downside depending on what you want

3 Likes

I guess it might be better to use attributes instead, I was just looking for a viable way to do this using modules

2 Likes

I don’t know what’s the problem here.
You can perform arithmetic on the fields of a table returned by a module without changing/affecting the base module.
Whenever a ModuleScript is required using require, it returns a clone of the value (in this case a table) returned by the ModuleScript.

2 Likes

I mean you could. My first extinct was to use lua OOP.

3 Likes

Okay, I think I know how to implement this into my system now.

2 Likes
-- Using Katie's module script here you can do this

local Data = {}

Data.BULLET_DAMAGE = 10
Data.BULLET_SPREAD = 2
Data.BULLET_TRAVEL_DISTANCE = 150
Data.BULLETS_PER_ROUND = 1
Data.BURST_DELAY = 0.05
Data.BURST_SIZE = 3
Data.FIRE_MODE = "Semi-Automatic" -- Only accepts "Semi-Automatic", "Automatic", or "Burst."
Data.MAGAZINE_CAPACITY = 12
Data.RATE_OF_FIRE = 0.08
Data.RECOIL = 0.02
Data.RELOAD_TIME = 2.5

return Data
local DataModule = require(game.ReplicatedStorage.DataModule)
local Trait = -- Insert trait here (if its a tool this event would work)

Trait.Equipped:Connect(function()
DataModule.RATE_OF_FIRE = -- New fire rate insert here
end)

This will affect the rest of the scripts aswell by the way.

1 Like

The only problem is that I’m requiring this module from multiple scripts, so how will those other scripts detect the changes? My gun system has a main controller module (which is client-sided) and then it does raycasting and all important stuff on the server, which BOTH the server and the controller module needs to know the updated gun data values.

1 Like

this should work fine, but the module value will have to be updated in both run contexts if the module script is being used by server and local scripts. if you modify a module value in a server script, it will not be modified in a local script using the module.

1 Like

Yeah it works. My script works:
image

I multiplied ROF by 100 and it applied accross scripts.

scriptus.rbxm (1.6 KB)

3 Likes

Forgot to mention about run-contexts aswell.
If they are modified on the same context (ex: server), the changes would reflect on the same context.
If I modified the module on the server, the changes would reflect on the server and not the client.

1 Like

Okay thank you for your help (chars)

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