Potassium API
  • Environment
    • Cache
    • Closures
    • Console
    • Crypt
    • Debug
    • Drawing
    • File System
    • Input
    • Instance
    • Metatable
    • Miscellaneous
    • Scripts
    • Web Sockets
    • Actor
Powered by GitBook
On this page
  • checkcaller
  • clonefunction
  • getcallingscript
  • hookfunction
  • iscclosure
  • islclosure
  • isexecutorclosure
  • loadstring
  • newcclosure
  1. Environment

Closures

checkcaller

<boolean> checkcaller()

Returns whether the function currently running was called by the executor.

This is useful for metamethod hooks that behave differently when called by the game.


clonefunction

<function> clonefunction(<function> func)

Generates a new closure based on the bytecode of function func.

local function foo()
    print("Hello, world!")
end

local bar = clonefunction(foo)

foo() --> Hello, world!
bar() --> Hello, world!
print(foo == bar) --> false

getcallingscript

<BaseScript> getcallingscript()

Returns the script responsible for the currently running function.


hookfunction

<function> hookfunction(<function> func, <function> hook)

Replaces func with hook internally, where hook will be invoked in place of func when called.

Returns a new function that can be used to access the original definition of func.

local function foo()
    print("Hello, world!")
end

local fooRef = hookfunction(foo, function(...)
    print("Hooked!")
end)

foo() --> Hooked!
fooRef() --> Hello, world!

iscclosure

<boolean> iscclosure(<function> func)

Returns whether or not func is a closure whose source is written in C.


islclosure

<boolean> islclosure(<function> func)

Returns whether or not func is a closure whose source is written in Luau.


isexecutorclosure

<boolean> isexecutorclosure(<function> func)

Returns whether or not func was created by the executor.


loadstring

<function?, string?> loadstring(<string> source, <string?> chunkname)

Generates a chunk from the given source code. The environment of the returned function is the global environment.

If there are no compilation errors, the chunk is returned by itself; otherwise, it returns nil plus the error message.

chunkname is used as the chunk name for error messages and debug information.


newcclosure

<function> newcclosure(<function> func)

Returns a C closure that wraps func. The result is functionally identical to func, but identifies as a C closure.

local foo = function() return true end
local bar = newcclosure(foo)

print(bar()) --> true

PreviousCacheNextConsole

Last updated 2 months ago

If func is a Luau function (islclosure(func) --> true), the upvalue count of hook must be less than or equal to that of func.

Lua visibility rules