Hi,
My game is gonna be using guns, probably a lot of them. The way my guns work right now, they are tool based, running off scripts in each tool. All of these guns use the same scripts, the only thing different is the config, so i feel like having 1 universal module control all of these, is better than local scripts copy and pasted in each tool. The problem is, I dont use custom controllers like viewmodels, i use standard roblox tools, im also a noob when it comes to OOP. How would I start here? Or is this approach worse than the local scripts in each tools?
Unfortunately I can’t right out a lengthy response with some code samples, but the simplist way to get into this would be to have the weapon scripts get cloned to each weapon. And have those scripts connected to value objects.
Then you only need to edit individual scripts.
Just make sure that the scripts are transfered to the tools before they get cloned to a player.
That’s what im doing right now, I was asking if its possible to make some sort of universal loader for the gun client framework. For example in the gun client script instead of 1k lines ill have one line which will require the module and continue from there on, but im not sure how to make a gun framework for standard roblox tools, as im not using any custom controllers.
Do not clone scripts. Use module scripts and require them from a Local script and send the module the configuration module for the gun.
You can create a module and just pass in the tool itself. E.g.
-- ModuleScript
local GunFramework = {};
-- constructor for a new gun
function GunFramework.new(toolInstance, configInstance)
local self = setmetatable({}, __index = GunFramework);
self.Instance = toolInstance;
self.Config = require(configInstance); -- assuming your config is a module containing the gun data
toolInstance.Equipped:Connect(function()
-- equipped event code
end)
-- setup your other events and other code for your constructor
return self;
end
return GunFramework;
In your localscript for your tool:
local gunframeworkModule = require(gunframeworkPath);
local gun = gunframeworkModule.new(script.Parent, script.Configuration);
Could you provide an example of code?
Honestly, if you’re a noob to OOP making a modular gun framework would be a step too fast in my opinion.
Making guns on roblox that perform well is already an extremely complex task and making that modular to be able to be applied to many weapons requires atleast a mediocre understanding of OOP.
My advice would be to try and understand OOP on its own, outside of the concept of making a modular gun framework, and once you understand the basics of OOP you will be able to apply that towards making your framework, as it should come naturally.
Additionally, I’m not sure that these code samples posted above are any use to you if you don’t understand them, because if you need to change something about them you won’t know how.
tl;dr Focus on building up your OOP understanding until you feel capable enough to make a gun framework.
Managed to whip up a little gun module with some changes to your code, basically turned my local script into a module one and it works perfeclty. Thanks for the help!
Have found a new problem with this approach. If you use multiple guns, they break because apparently only 1 gun is able to use this module at all times, using multiple guns bricks this whole system. When I was using local scripts this was not an issue. Does anyone have an idea on how to fix this? Maybe a mix of local and module scripts? (module for functions, ill just call them in the local script, feel like this could work)
Edit: Fixed this by putting all the functions into the Tool.new() function