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?
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)
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
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.
-- 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.
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.
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.
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.