Skip to content

getcallingscript

Notes on getcallingscript

If a game script is executing, and getcallingscript is called, it must return the proper Script, LocalScript, or ModuleScript - even if the script global for said script is set to nil.

getcallingscript returns the Script, LocalScript, or ModuleScript that triggered the current code execution.

function getcallingscript(): BaseScript | ModuleScript | nil

Parameters

Parameter Description
(none) This function takes no parameters.

Example

Detecting the calling script in a hook
local old; old = hookmetamethod(game, "__index", function(self, key)
    if not checkcaller() then
        local caller = getcallingscript()
        warn("__index access from script:", caller and caller:GetFullName() or "Unknown")

        hookmetamethod(game, "__index", old) -- Restore the original
        return old(self, key)
    end

    return old(self, key)
end)

print(getcallingscript()) -- Output: nil, since we called from an executor thread