How can I make a global function,variable etc

How can I make a global function,variable etc.

3 Likes

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.

2 Likes

If your taking about global things then you would use _G but I advise against that

1 Like

here is an example

script1

function hello()
        print("Hello")
end

script2

hello()
2 Likes

You can just use a module script and store the hello function inside of it

local module = {}


function module.Hello()
   
end

return module
2 Likes

I looked at it but I cant rename _G and I am not trying to use modulescripts

2 Likes

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 :confused:

1 Like

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

1 Like

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

1 Like

but what is the reason behind not using module scripts to achieve something a module script does?

2 Likes

How can i rename _G like Hello = _G

_G.Hello = function()

end
1 Like

It is hard to tell but I want to rename _G globally

local GlobalFunction = _G

GlobalFunction.Hello = function()

end
1 Like

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