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.
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.
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
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).
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
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.