How can I make a global function,variable etc.
What do you mean? Do you mean one that can be used across multiple scripts, or one that can be accessed anywhere from one script?
You want a module for multiple scripts, and just don’t put local
for normal scripts.
here is an example
script1
function hello()
print("Hello")
end
script2
hello()
You can just use a module script and store the hello function inside of it
local module = {}
function module.Hello()
end
return module
I looked at it but I cant rename _G and I am not trying to use modulescripts
You need a module, then. Insert a ModuleScript
. _G and module are the only two ways really.
In the module:
local module = {}
function module.hello()
print("hello")
end
return module
then, require it:
local mod = require(path_here)
mod.hello()
@astraIboy beat me to it
Why dont you want to use module scripts though, they are exactly for this purpose
I am working on a project and I shouldnt do it
But the best practice is to use module to store functions you’ll be using in various scripts
I know how to use modulescript i just need to use functions globally
Okay well the only other way you can do that is through _G
Also keep in mind _G only works on the same context level so that means if you have a global function on the client you can’t use it on the server and vice versa
but what is the reason behind not using module scripts to achieve something a module script does?
How can i rename _G like Hello = _G
_G.Hello = function()
end
It is hard to tell but I want to rename _G globally
local GlobalFunction = _G
GlobalFunction.Hello = function()
end
That probably won’t work and also why are you trying to rename _G
, I dont even think you can rename globals you can only assign them to variables
sript1
Hello = _G
Hello.MyNum = 10
script2
print(Hello.MyNum)
And I know it is possible because a youtuber did it