How is it possible to only have 1 local and 1 server script in a game with multiple weapons?

Currently have a sword fighting game, where you can customise your weapon. The look of the weapon does nothing bonus to its status, purely cosmetic. However, I have to create a separate tool for each weapon so that the grip position is correct for each one. This also means that there’s multiple tools with the exact same scripts in them (each have a server and local script)

How can I go about making it more modular? I always hear talk about how a game should just have 1 server and 1 local script that handles every single module, but that to me makes no sense. However, I’d like to make the weapons more modular as if I want to add something or a bug occurs, whatever, I’d have to go through dozens of scripts to change something, as they are all just the same script


1 Like

If I was you, I’d make a script in SSS that would control the weapons. One for each type of service (Shops, Weapons, Vehicles etc)

Have a local script fire to an event and handle it in that one weapon script.
As for the grips, I’d probably make a module and link the correct tool or grip position and allow the script to find that part of the module and use the settings provided to suit your tools needs.

Edit: There’s probably better ways of doing this, such as what @AbiZinho just posted. Just giving my input

I use the AeroGameFramework system by Crazyman32, which is much more organized and simpler (once you get the hang of it) to use than my old method. It is completely modular, and is much more network friendly, as modules only work when a module requires another module.

It also takes care of the remotes for you. If you are interested, I’d definitely recommend watching the tutorials given in the thread (linked above).

7 Likes

Yea, I’ve seen a few stuff from it. However, I have basically already coded the entire game, so no point switching systems now

1 Like

Usually, how there ends up to be only 1 local script and server script is that everything (that is code) is done is under the hierarchy of some type of event or function such as a PlayerAdded or even a local function, so you don’t expect anything to run without permission from/ calling the function. So all these functions will end up in the same script, and we don’t expect performance issues because everything is controlled under a function.

Well, this is what I come to think of, at least.

2 Likes

I actually develop for a game that uses a single LocalScript and Script to host all it’s guns in a modular fashion. I honestly don’t recommend it, but whatever works for you.

Layout:
Script in ServerScriptService
> Handles replication and important functionality
LocalScript in StarterPlayerScripts
> Handles client-side logic, equipped events, animations, whatnot
Folder in ReplicatedStorage
> Holds tool data modules

The tools themselves don’t have any scripts. When a tool is added to the backpack, the LocalScript in StarterPlayerScripts hooks it’s logic to the tool’s respective events and registers them for further functionality.

1 Like

It’s never too late to rework a system. However, I’d wait until you have a lot of time to do it in sessions. I’m currently redoing the entire combat system for my game for the exact same reason as this post. Like it’s already been said, modules would be the way to go. My advice is: if you have to remake something to make development easier, you’ll have a better time making the game overall.

1 Like

When you make your game you want to have multiple modules and as few scripts as you can.
Why modules:
People use modules so that you don’t repeat your self when you code for example you could have one function in a module script then call it multiple times in the server script. This then makes updating your code a lot easier also makes coding a game a lot quicker. If you are copying and pasting code you should put it in a module script.

How is it done:
From my understanding you should put your loops in coroutine or a spawn function so that you can have multiple loops running at the same time without the script stopping. This could be totally wrong so please correct me if I am

In your case you could have a module script that has the settings for the tools, e.g: The grip position for each tool would be in that module. You could have another one with the animations of the tools in it.

1 Like

Why don’t you store all the grip positions in a dictionary and have one weapon tool with your script inside. When you give the player their weapon, clone the sword into the weapon tool and set the grip position to the one in the dictionary for the sword.

2 Likes

coroutine or a spawn function

If it’s just one script handling multiple calls that either has wait times or non-immediate return then yes, do this. Having them run in different threads will prevent some wacky behavior. There are however other ways to implement this that don’t explicitly use coroutines our spawn, but the underlying principle is the same. In this situation function calls must be asynchronous, much how like web servers handle traffic and stuff.

1 Like

Personally, I store all item data inside of a module. Whenever you give a tool to a player, duplicate a “weapon” object (a tool with all the scripts inside, I’d store this in replicatedstorage), and set its properties based on the information in the module. Example:

WeaponData = {
    ["Pistol"] = {
        GripPosition = Vector3.new(0,6,0);
    };
}
1 Like