Tool script sometimes doesn't require modules when parented by a code?

Alright so, I am trying to make something in Roblox. It’s an FPS framework and it might become a game once my code is officially stable and ready for game use. Now the the gun itself is working fine including the FPS arms itself.

The tool itself contains the following:

  • The weapon itself
  • The code that will handle everything including FPS arms.

First of all I have the code require two module scripts.
First is the ModuleScript for the Configurations of the weapon. That will include animations for the FPS arms. Second is another module that is the system for the weapon itself. Handling every mechanics for the gun.

The modules get required when I already set the parent of the Tool in StarterPack. But when I set it to ReplicatedStorage then clone it to the player’s backpack it just won’t require it.

You can watch the video of the problem here: 2021-05-15 18-18-49

Basically this is what it looks like in the explorer:

The screenshot when I just place the Tool in StarterPack:
image

Now I parent it somewhere else like ReplicatedStorage:
image

So what happens is I need it to work when a code clones the Tool into the players backpack but it won’t work. Resulting the code into not requiring the System module aforementioned earlier.

It does require one module which is the Config one but it doesn’t require the second module which is the system for all the gun mechanics and stuff.

So I have a clickable part to make a copy of the Tool and parent it to my Backpack. The code:

script.Parent.MouseClick:Connect(function(player)
	local Tool = game.ReplicatedStorage.M4A1:Clone()
	Tool.Parent = player.Backpack
end)

Looks pretty straightforward right? Now when you look at the part of the vid where I click the part. Yes it works, it does clone the tool then parent it to my Backpack. Now I equip the tool, it doesn’t even work. It doesn’t import the FPS arms and other stuff because it’s due to the fact that the code hasn’t even required the second module yet. In order for everything to work, the gun needs it’s two modules.

I checked the script in-game, it does exist there.

Ignore the name of the script btw. That’s just a name. Anyway, if you watched the whole video, you’ll see the tool is parented in StarterPack first, I equip the gun it works. Now I parent it to ReplicatedStorage, have the clickable part do the job. I equip the gun, doesn’t work anymore.

I even tried printing and the only module that is being required by the code is the Config. Here’s the screenshot:

You can see in the output, the Config module has been required by the localscript successfully while the system module is not required.

This is how I require the modules:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local GunModules = ReplicatedStorage:WaitForChild("GunModules")

local Config = require(GunModules.Settings.M4A1)
local System = require(GunModules.System.M4A1)	

Looks fine right? It looks like I typed it correctly. But I don’t get it why it doesn’t require the second module script.

For further suggestions and questions regarding this, reply below.
You can contact me through Discord: Duckrollerist#8679

I don’t understand why the behaviour of the module loading would change, as no script or localscript will run when parented to ReplicatedStorage .

Are you trying to use ReplicatedStorage as a repository for the ModuleScripts? If yes, then they don’t need to be contained within a copy of the Tool in ReplicatedStorage and would be better kept within a shared folder in ReplicatedStorage to be accessible by both Client & Server. If they don’t need to be accessed by LocalScripts, then don’t keep them in ReplicatedStorage as they can be accessed by the client and can be disected and exploits become discoverable.

See these 2 articles:
https://developer.roblox.com/en-us/api-reference/class/ReplicatedStorage

1 Like

@BadDad2004 First of all I have a folder called “GunModules”. Inside that folder it contains two more folders. A folder named “Settings” and “System”. Inside those two folders contain a modulescript named “M4A1”. Those are two separate modules that belong to their respective folders.

First one is for the mechanics and everything. Second is for the configurations of the gun.

@BadDad2004 if you’d like to have a TeamCreate with me add me on Roblox so you can take a look at the explorer, code and stuff.

I’ll stay away from TeamCreate for now and probably wise for you not to offer it to other people on here either. Not everybody in this world is as friendly as they make out to be. Be careful.

OK, so we know that to Tool works when parented to StarterPack, good. I personally think there is no reason for it ever to exist in ReplicatedStorage storage. Move it to ServerStorage, then when the play is given the weapon, clone it from there to their Backpack, or direct into the Character.

I handle most of my weapon functions by moving directly into the character when they fire an Equip event to the server. I also use ModuleScripts for common weapon functions and this process works OK for me.

1 Like

If it doesn’t require the second module script then it’s possible that it doesn’t run at all, check for yields and circular requiring within the module script.

Also keep in mind require only runs once so it might be possible you required it in another script in the same environment (server or client environments)

1 Like

@dthecoolest The second module does run. If the tool is parented in StarterPack, I play the game, equip the gun, it works.

Take a look at the code I provided above. Those two require functions are in the same place. I didn’t require them anywhere else but require them in a localscript.

@BadDad2004 Should tools be always parented in ServerStorage?

That is where I store them in-game and clone them out from there. Other people might handle it differently.

1 Like

@BadDad2004 I changed the tools parent for cloning. It still does the same. I don’t get it, why is the second module not being required…

The second module only gets required when the Tool is in StarterPack. Because when you play the game, the code will run immediately.

OK. Quick test to confirm that theory. Set the script within the Tool to Disabled and place the Tool into ServerStorage.
As part of the equip process, Clone the Tool to the player, then Enabled the script. This will then force it to pickup all it’s required modules from the start.
I can’t explain the behaviour you are seeing.

1 Like

OK, I just checked a game where I have a common weapon module and it seems I do Enable the main weapon control script when it is cloned to the player.

1 Like

@BadDad2004 I will try that method and let you know!

@BadDad2004 Big RIP. It still doesn’t work. 2021-05-16 20-19-20 Take a look at the video.

It literally only works on StarterPack like why. If it’s on starterpack, it will require the two modules. But if it’s parented by a code to the backpack, it doesn’t require the second module. Weird.

OK. That doesn’t make a lot of sense. In the the System M4A1 module, line 4, you reference the GunModules folder. Is this actually referenced anywhere in the script? Going back to @ dthecoolest, you might have circular references/requires.
Cool to see the vid btw, it really helped show the issue and how it all hangs together.

1 Like

@BadDad2004 GunModules is a folder inside ReplicatedStorage containing two folders. Settings and System. Settings folder will contain the Config module while the System will contain the system module.

image

You can see, there are two separate modules that belong to their respective folders.
Now, line 4, containing this:

local GunModules = ReplicatedStorage:WaitForChild("GunModules")

That’s referred already on the top part of the System module. I set variables and place them to the very top of the code.

I just wondered whether within a specific Module, do you have references to the other Module? Which in turn, may have a reference back to the fist module. Ad infinitum.

@BadDad2004 alright this is starting to get confusing. The reason that I offered you team create is for you to take a look at the codes.

I think module is requiring another separate module. Lemme check first.

EDIT: I don’t think so.

I agree that it doesn’t make a lot of sense and it becomes very frustrating when you come across these sort of things. I really do not understand why you are getting the behaviour you are, when the scripts and modules all seem to work OK when in StarterPack.

I will say again not to offer team create to people on the DevForum, as not everybody in the world is nice and a nasty could easily introduce unwanted code into your hard work. Be careful.

Can anybody else shed any light one this?

1 Like

add a wait at the beggining of the scripts that require that module scripts for the tool.
An example is:

local tool = script.Parent

tool.Equipped:Wait()

Make sure to put this at the very beginning of your script before anything else and to not have any wait functions before requiring the modules or connecting functions. What your problem is when dying with the tool is that the scripts try to load in the animations on the view model and or player’s character before they are parented to something in the workspace, adding a wait would fix that. As for your second issue, I think this will work as well.

1 Like