# Closures

## checkcaller

```lua
<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

```lua
<function> clonefunction(<function> func)
```

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

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

local bar = clonefunction(foo)

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

***

## getcallingscript

```lua
<BaseScript> getcallingscript()
```

Returns the script responsible for the currently running function.

***

## hookfunction

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

Replaces `func` with `hook` internally, where `hook` will be invoked in place of `func` when called.&#x20;

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

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](http://www.lua.org/manual/5.1/manual.html#2.6)

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

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

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

***

## iscclosure

```lua
<boolean> iscclosure(<function> func)
```

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

***

## islclosure

```lua
<boolean> islclosure(<function> func)
```

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

***

## isexecutorclosure

```lua
<boolean> isexecutorclosure(<function> func)
```

Returns whether or not `func` was created by the executor.

***

## loadstring

```lua
<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

```lua
<function> newcclosure(<function> func)
```

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

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

print(bar()) --> true
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potassium.gitbook.io/api/environment/closures.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
