How can I make a global function,variable etc

it is possible as its a table shares across the same script context.
this table is not safe to use though but you are indeed making a global “variable” like that.

You must be misunderstanding the youtuber’s script then.

You can do:
Script 1:

function _G.hello()
   print('sus')
end

Script 2:

local hello = _G.hello

hello()

You can’t do much better than that.

1 Like

If you rlly wanted to:

Script 1 -

local TableName = _G
TableName.variableName = 'value'

function TableName.funkyName()
 print('funky')
end

Script 2 -

local TableName = _G
print(TableName.variableName)
TableName.funkyName()
1 Like

if your not using module scripts, try bindable functions, its the only metgod i use except modulescripts,

edit: this wont become global just accessable outside the script

This guy doesn’t want to bother learning module scripts and wants to use an easier way. Trust me bro, module scripts aren’t all that scary.

1 Like
set0 = 1 --script global 
set1 = nil --preset script global --something used later but set up as valid 
local set2 = 1 --script global --same as set0

function setup()
  local set3 = 1 --function global --global only to the function
end

local rs = game:GetService("RunService") --a globalized constant
rs.Stepped:Wait()

_G.set4 = 1 --project global --this can get sloppy

--different script in project
print(_G.set4)

First off I would recommend module scripts, but for a global variable you would do something like this

_G.Text = "Hello World"

But for a global function you can do something like this

Script One

_G.Function = function()
 Print("Hello World")
end

Script Two

_G.Function()

To add on it will be locked to ether the server (If created on the server), or the client (the client you created the variable on)

There is two options, but I would recommend using option 2. It helps optimize and store your variables.

Option 1:

To achieve this you could use _G. Here’s how you use it and here’s how it works:

How do you use it?

If you wanted to use this, you could do the following:

_G.VariableName = 'Value you want'

from there you can access this variable by doing this:

print(_G.VariableName)

What will this do?
This will automatically make a variable in every script that you could use.

Option 2: (Recommended)

To achieve this, you could also use module scripts. These helps optimize and store your variables easier. I would 100% recommend using this method.

How do you use it?
You could utilize module scripts by one, making a module script and typing this:

return {
  VariableCon_I = {
   Variable_I = 'Value1',
   Variable_II = 'Value 2',
   Variable_III = 'Value 3',
  },
  VariableCon_II = {
   Variable_I = 'Value1',
   Variable_II = 'Value 2',
   Variable_III = 'Value 3',
  },
}

--[[
it may look hard but trust me you will get the hang of it if you keep
using them in your games. I suggest you practice or study them if you are not
that familiar with these particular scripts.
]]

To get the values within this module script you could do the following:

-- assuming the local script or script is under the module script

local mod = require(script.Parent) -- get full access to the raw code
print(tostring(mod.VariableCon_I.Variable_I)) -- prints one of the values

And once again, this may look hard but you will eventually get the hang of it.

Direct use of global variables is typically discouraged for better code organization and modularity.
I think/would say this is more of a have to know what you’re doing well thing. Something to stay away from when you start but embrace when you’re polished. See what Roblox has to say about it.

Yep, I stated in my post that I recommended he used module variables instead.

It is there so …
I know it can make a mess and make error checking a nightmare. Some languages use a lot of serializing and you can do that on Roblox too. _G is directly related to serialization.

1 Like