Any Problems with this code?

Hello! I’m making a game that loads using a Driver Module Script, but the script wont run past this piece of code:

do
	local inits = {}
	
	for k, plugin in pairs(_p) do
		if type(plugin) == 'table' and k ~= 'Chunk' and plugin.init then
			table.insert(inits, plugin)
		end
	end
	
	table.sort(inits, function(a, b) return (a.initPriority or 0) > (b.initPriority or 0) end)
	for _, plugin in pairs(inits) do
		plugin:init()
	end
end

Any ideas on why this is happening? Or how to fix the code so the script will run past this piece of code?

1 Like

are there any errors displaying in the output?

No errors are displaying, I did have this issue earlier in the script but it was just a :WaitForChild() trying to locate something that did not exist. But in this case I do not know why there would be anything stopiing the script.

I do hope at least when it was the :WaitForChild issue, you did get an error with that. If you didn’t then something might be wrong with your installation of studio, and you might not be getting errors for other things as well.

Have you put any print statements, or break points in that section of code to see where it is actually stopping?

Is it even getting to any of the for loops?

See if you can pinpoint more precisely what is the last line it executes before it becomes ‘stuck’

1 Like

Yes I used prints to see where exactly the issue was happening,

print("ggg")

do
	local inits = {}
	
	for k, plugin in pairs(_p) do
		if type(plugin) == 'table' and k ~= 'Chunk' and plugin.init then
			table.insert(inits, plugin)
		end
	end
	
	table.sort(inits, function(a, b) return (a.initPriority or 0) > (b.initPriority or 0) end)
	for _, plugin in pairs(inits) do
		plugin:init()
	end
end

print("ggg")

When the script is running, only 1 out of the 2 “ggg” prints appear in the output bar, and other :WaitForChild() Errors occur, so I don’t think something is wrong with studio.

But I’ve only found the area it gets stuck.
And like you said I do still need to find the exact line the script gets stuck. So I’ll investigate now.

Okay so I put a print line in every line of code in the area the code is getting stuck, and I found the script stops exactly after the area for some reason. Any ideas?

So you mean it stops exactly after the last ‘end’ that closes the ‘do’ statement?

It stops right here:

for _, plugin in pairs(inits) do
		plugin:init()
	end
--Here
end

Try to use next instead of pairs make a print of any exception (any value), do the init causes it?

1 Like

After a call of init inside a coroutine you can use debug.traceback to know the exact address of ~error~ (if it).

Okay, I now know the exact location of the error:
It is this line right here:

plugin:init()

The function is from a module with this code:

return {}

And I assume that is because the module script contains a lot of module scripts with the same :init() function held inside.
Here is what the module script looks like when expanded.


and the plugin asset the module is calling on to use the plugin:init() function is listed farther up in the modules code: (the module I’m trying to fix)

local pluginsModule = script.Plugins
pluginsModule.Parent = script.Parent
local _p = {}

I don’t know if this helps at all but I thought I should send all these details.

You got a lot of those! Maybe you can try using task.spawn to do it asynchronous. And also if it is a silent crash/error you would like to do this:

for _, plugin in pairs(inits) do
    local thread = coroutine.create(plugin.init)
    -- assert resume
    local success, m = coroutine.resume(thread, plugin)
    if not success then warn(debug.traceback(thread, m)) end
end

I replaced the plugin:init() line with your code:

do
	local inits = {}
	
	for k, plugin in pairs(_p) do
		if type(plugin) == 'table' and k ~= 'Chunk' and plugin.init then
			table.insert(inits, plugin)
		end
	end
	
	table.sort(inits, function(a, b) return (a.initPriority or 0) > (b.initPriority or 0) end)
	for _, plugin in pairs(inits) do
		local thread = coroutine.create(plugin.init)
		-- assert resume
		local success, m = coroutine.resume(thread, plugin)
		if not success then warn(debug.traceback(thread, m)) end
	end
end

But now this line,

if not success then warn(debug.traceback(thread, m)) end

Yields this error:


I’m really sorry, I haven’t done the task.spawn part of it mainly because I don’t really know too well what that means or how to do it, I’m probably doing this wrong so whenever you have a chance could I get a more in-depth explanation on what to do? Again I’m really sorry for kinda being a bit of a pain.

Don’t worry, probably I written the code bad because I’m in my cellphone answering those questions:). So if it could be a execution error this part of script will throw all information and location of it, like a pcall but more detailed.

Can I ask, do you replace debug to another value?

I did not, I simply just copy pasted your code into the module. I may need to do further coding past just copy pasting the code, or like you said, use task.spawn.

More “detailed” info of what traceback do here.

So what decision you take or what you don’t feel like the past you say. Better continue improving your code and maybe you found the answer behind your eyes

I just tested a few more things and the script works now! Thank you for your time!

1 Like