Debug.Info returns wrong script calling a function when the function is returned from a module

Reproduction

DebugInfoBug.rbxl (53.5 KB)

The above file contains two scripts (server scripts), Script1 and Script2, and one module.

Module:

return function()
	print("Module", debug.info(2, "s"))
end

Both scripts:

local Module = require(script.Parent.Module)

Module()

Expected behavior

Expected vs Actual Output

It is expected that both ServerScriptService.Script1 and ServerScriptService.Script2 would be output, however only one script is output twice.

Expected Output:

Module ServerScriptService.Script1
Module ServerScriptService.Script2

(It is expected that the order of this output would be random)

Actual Output:

Module ServerScriptService.Script1
Module ServerScriptService.Script1

The output script randomly changes between Script1 and Script2 depending on whichever is executed first.

8 Likes

Hello.

Roblox engine reuses data of identical scripts to reduce memory use.
Because “s” refers to the “source” property in the compiled script data, the same value is returned for all identical scripts.

script variable can still be used to get the engine Instance of the script, which is still different in each script.

I assumed it was something like this, however the behavior isn’t documented anywhere I could find and is very unintuitive behavior.

I would like to have this for my networking library Red, where you write events into replicated modules and the module’s name/path are used as the event’s name. Passing the script global or a string name are both possible solutions, however they add extra work to the API’s consumer.

If this is something that can’t change I understand, however the documentation should make this behavior clear.

As far as your repro is concerned, inside Module, you can swap debug.info with debug.getmemorycategory.

New code:

return function()
	print("Module", debug.getmemorycategory())
end

Output:

Module Script1
Module Script2

I’d link the documentation for debug.getmemorycategory; however, it doesn’t currently exist.
The description for debug.resetmemorycategory should suffice.

1 Like