Accessing values from a configuration in a ModuleScript

Hi there!

Never used configurations before so I’m a bit confused on how they work.

I’m trying to create a first-person weapons system, of course with some functions (damage, raycasting) happening on the server and others (viewmodel, effects, keybind stuff) on the client. In the actual weapon model itself, I’m going to use a configuration to store values like max ammo, current ammo, damage, all those weapon-specific variables. Just makes things a bit more organised, also it’d be nice to know how to use them in case they’re useful in a future project

This is my hierarchy at the moment, with stuff which matters circled in red.

Given that every weapon will have the same variables with different values, how would I access the configuration variables that are within the weapon (in StarterPack) in WeaponSystemServer? If it was done as variables defined in enableWeaponServerside, it would be easy enough, but if it’s a configuration I’d guess the configuration itself would have to be defined in the WeaponSystemServer somehow rather than passed through a function in enableWeaponServerside?

Any help would be appreciated. This is probably really simple but I’m completely clueless when it comes to using this kinda stuff.

1 Like

Are you trying to change any values inside of a weapon inside of a player inventory? Then they should be in their Player.Backpack rather than game.StarterPack.
It is still slightly unclear what you are trying to do here.

Maybe I overexplained a bit.

Pretty much I just want to be able to access the properties in the configuration from the ModuleScript. I’ve just never worked with configurations before and there’s no documentation on how to do that from what I’ve seen.

I wasn’t aware of the Player.Backpack thing though, thanks!

The Configuration is just like a Folder as far as I’m aware (basically a container), it is holding value instances in your case. To read these values you have to use the following for example:
local AmmoCapacity: number = Tool.Configuration.AmmoCapacity.Value

So it’s easiest just to have something like this in enableWeaponServerside

local WeaponSystem = ServerScriptService:WaitForChild("WeaponSystem")
local WeaponSystemServerside = require(WeaponSystem.WeaponSystemServerside)

local configuration = script.Parent.Configuration

local function handOffVariables()
	WeaponSystemServerside.AmmoCapacity = configuration.AmmoCapacity
end

With a corresponding variable in the Module Script like:

local WeaponSystemServerside = {}
WeaponSystemServerside.__index = WeaponSystemServerside

function WeaponSystemServerside.new()
	local self = setmetatable({}, WeaponSystemServerside)
	
	self.AmmoCapacity = nil --(or a predetermined default value for if it breaks)
end

return WeaponSystemServerside

I was more hoping for a way to cut out having to define it in the serverscript and just have the modulescript grab it directly in some other way, however this is probably the easiest way. I’ll do this for now unless I end up finding something better.

If you are seeking to automatically update a variable, you can use the .Changed event
Consider A to be any Value object in this scenario and its corresponding variable B
A.Changed:Connect(function()
B = A.Value
end

Ah I think I see where you’re confused

This is (mostly) not about updating variables, it’s when the player initially picks up the weapon, the values under the configuration need to be known by the ModuleScript in order for it to know how and when to handle weapon actions like firing, reloading and dealing damage.

I can’t define the tool directly in the module script until it gets picked up as that code has to be used by all weapons - so I was hoping there was a way for the ModuleScript know there is a configuration within the model for the tool and access it directly, instead of passing all the configuration values through a function in the ServerScript.

I would only update configuration values (pretty much just ammo) when enabling and disabling the weapon, probably through RemoteEvents or something, the rest would be done directly in the ModuleScript.

I am still confused about what you’re trying to achieve. Why can’t the module script define the weapon until it is picked up? Why wont you define the variables then when the tool is picked up?

ModuleScripts can run code just like any other script, it will cache its value for all scripts in the same environment and may have access to the local environment of the script that requires it.

The modulescript is meant to handle the basic functioning of all types of weapons - e.g. pistol, sniper rifle, smg - using formulas with the variables given by the serverscript for whichever specific weapon is being held at the time, similar to the Roblox weapons framework (although thats much more complicated than what I want and also is third person only, unfortunately). It’s more efficient and organized, and also more secure against exploits

Essentially:

Weapon is equipped and the information for that weapon is sent to the module from the ServerScript → Module stores these variables → Clientside, an action is performed (e.g. firing the gun) → Module handles this action and can perform the action, without needing any code specific to the weapon

Also, since there could potentially be several instances of a weapon existing on the server, every single instance of every single weapon type being defined would be too much. There needs to be a backend framework which can handle the calculations and actions of the gun without being weapon-specific.

Hopefully this explains it

I am sorry but I might still not exactly understand what you’re trying to achieve, it might be too specific for me to understand.

There being too much weapon references isn’t too bad I think; you can reference thousands of items without much loss in performance.

I need to rest now, but I hope you will find your solution.

No worries, thanks for trying to help anyway

For now I can just do what I said above anyway, it still works it’s just not ideal