Require() Stops Script

Hello devs! I have come across a bug in my script that I have no idea how to work around.
The script:

PlayCurrentDialog.OnClientEvent:Connect(function(Name)
	print(Name) --Prints
	local DialogModule = require(game.ReplicatedStorage.Dialog[Name])
	print("Required") --Doesn't print
	local DialogTable = DialogModule["Current Dialog"]
	DialogFunctions["PlayDialog"](DialogTable)
end)

There are no errors in output.
I have also checked if the modulescript exists and it does:

local ModuleScript = game.ReplicatedStorage.Dialog:FindFirstChild(Name)
if ModuleScript and ModuleScript:isA(ModuleScript) then
	print("Exists") --Prints
end

Is there anything I can do?

1 Like

use ReplicatedStorage.Dialog:FindFirstChild() instead of ReplicatedStorage.Dialog[Name]
you should also use an if statement to make sure it finds the module, and if it does require it :^)

1 Like

Whats the module code? Because a module must run all the code in it before it returns so if you have a loop that does not end. Require wont work. It will just pause the thread forever.

i think OP is just using the module to store a table with dialog

1 Like

The module stores a few nested tables with dialog.

Try putting a print in the module and see if it runs when you require it.

1 Like

Didn’t change anything but I’ll start using :FindFirstChild() thanks!

The code in the topic has a few prints, one after the require and it doesn’t print:

I mean put a print statement in the module and see if it prints when you require the module in another script.

What is likely happening here is that ā€˜Dialog’ is an instance, and when indexing it with ā€˜Name’ what you’re actually doing is getting the name of that instance, and not getting the child of that instance. Either change ā€˜Name’ to something else, like ā€˜Module’, or use :FindFirstChild(Name)

Have you tried printing what game.ReplicatedStorage.Dialog[Name] actually is?

1 Like

[] will find a child or value of an object/list, it won’t give a property afaik

Okay I identified the problem: inside my ModuleScripts I was requiring other ModuleScripts (which causes an infinite loop like @ineed_massnow said) , I used Bindable Functions so the ModuleScripts could communicate with each other without breaking the main script.

1 Like

i’d also recommend using game:GetService("ReplicatedStorage") rather than game.ReplicatedStorage (this can be done for all services)

1 Like

It does give a property if the query is a property name. This method is pretty useful in certain situations (storing a number of properties in a table and then iterating through said table to change properties of an object).


It actually checks for a property before checking for a child instance.

For example, if you put a folder into workspace and name it ā€œNameā€ then:

print(workspace["Name"]);

will print out ā€œworkspaceā€ in the output window, disregarding the folder that you created.

1 Like

However this does not apply to the script I had written due to Name being a variable and instead of:

print(workspace["Name"]); --Prints property

It is:

local Name = "FolderName"
print(workspace[Name]); --Prints folder

I know, I was referring to the comment of:

which seemed to imply that this is the case in general (outside the scope of your own script).

1 Like