Piece of code randomly breaks when playing

Hello! I am making a script that runs a game I’m working on, but this piece of code will randomly work, and randomly break upon joining.

for _, module in pairs(pluginsModule:GetChildren()) do
	local plugin = require(module)
	if type(plugin) == 'function' then
		plugin = plugin(_p)
	end
	_p[module.Name] = plugin
end

Any Ideas why?

Try putting print type(plugin) before your if section to see whether or not it’s the code inside the if statement that’s breaking, or something outside.

Here’s what the output looks like before it breaks:
Screenshot 2022-10-19 145640

This is what the output looks like when the game works occasionally:

It could possibly be because you ran it too soon. Try running the code after everything loads…? From my experience the reason why some things are working but also not working is because you run it too soon. I dont have a direct answer but this should help you probably

you need let it load first or it will become nil

wait(2)
for _, module in pairs(pluginsModule:GetChildren()) do
	local plugin = require(module)
	if type(plugin) == 'function' then
		plugin = plugin(_p)
	end
	_p[module.Name] = plugin
end

I tried this and it still occasionally breaks. Any other ideas?

@CTGVN123 you should use WaitForChild instead in case of lag that’s over 2 seconds. It will pause the script until the item required is in the workspace or folder it’s supposed to be in.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.