Creating a custom global function in Roblox Studio (without modules or _G)

Hi everyone,

In Roblox Studio (Lua), there are several built-in global functions like wait(), print(), and others. I’m trying to create my own custom global function that I can use throughout the entire game, both on the client and the server.

The key requirement is that I don’t want to use modules or _G variables. I just want to call a function like kill() anywhere in a script.

I understand that there might not be an official way to do this and it could be challenging, but I’m wondering if there might be an alternative approach.

Example:

local plr = game.Players.LocalPlayer
local char = plr.Character

kill(char)

in the background it will first take the character argument, and find a humanoid in it, and then kill it.

5 Likes

Why would you not just use modules

5 Likes

I don’t want to require it always in every script.

It’s like you have to require the print function in every script where you have to use it.

4 Likes

Im pretty sure bindble events allow this but bindble events only work on the sever. But they do yeild

4 Likes

It’s better than having to retype the same function in each Script. The purpose of a ModuleScript would be to keep functions being reused in multiple scripts to be easily edited in 1 spot.

2 Likes

Bindable Events can communicate Client-Client and Server-Server, basically opposite of RemoteEvents.

2 Likes

Oh ok but i was just half wtching a video and scripting something else random at the same time

3 Likes

Cant you just require it once then that one module has all the functions you need?

local mod = require(MODULE_PATH_HERE)

mod.kill(char)
3 Likes

Actually languages like JavaScript do this with some of their global functions.

console.log()

document.cookies()

But to be fair these aren’t “Global Functions” they’re added by your browser.

3 Likes

false bindable events work both on the client and server. the thing is that if a client fire a bindable event that is binded to a server script it will do nothing

4 Likes

I dont think this is possible, i think the more close you cant reach for that is creating a module and storing their function on a variable like:

local LifeModule = require(somewhere)

local kill, respawn = LifeModule.kill, LifeModule.respawn

kill(player)
task.delay(5, respawn, player)
4 Likes

Can confirm this works just tested it in studio.

3 Likes

3 Likes

Even if this was possible with some metatable stuff, the function would need to be defined somewhere, leading to a race condition if another piece of code tries to use the function before it’s defined elsewhere.

You should just use a module so that the script waits for the module to define the functions. You can make aliases by setting local variables to the module’s functions (e.g. local kill = module.kill).

If you’re really set on not wanting to type module.kill you can just copy and paste the require and setting code to the top of all your scripts, or add a plugin to automatically do that when a new script is added.

If you’re using a function so many times across many scripts you might have too many scripts and should consider using a different setup (e.g. use the collection service and set up all things of one type at once).

1 Like

Modules are the best way to go around it, requiring the module takes literally one line and is way easier and powerful then doing it any other feasible way

Have you considered using modules and _G in tandem?

Having a server script that requires every relevant module you have and stores them in _G, allowing you to simply call from _G when you need to. You’d be able to do something like _G.Kill(chr) in any script, which is as close as you can get to what you’re looking for from what I can see.

Edit: setting this up should be self explanatory if you know how both work but just remember that your scripts have to wait for _G to get all the modules required if you end up doing this

1 Like

You came all this way, took the time to write out a post and even replied, when if you just used your curiosity and brain you’d have found your own official roblox answer documented on the wiki:

You can access global variables and functions within the same script, but not between multiple scripts. Therefore, a global variable or function doesn’t provide any benefit over an in-scope local equivalent, an upvalue, or a shadow.

“But thats not what I wanted-” who cares. It’s a bad idea. Objectively.

Ya, you could do some shenanigans with newproxy and metatables, but that is quite literally less inefficient and slower than if you just did it normally, so im withholding the links to those resources. You can dig more if you’re that desperate to be wrong.

1 Like

but I want to require it once, and then use it in every script.

but with modules I have to require in in every script where I want to use it.

is there maybe a way without G?

like the function print()

you dont have to write like _G.print()

But I dont want to require it in every script,

I just want to write kill(character)

just like: “print()”