_G vs shared - What's the difference?

I meant it’s cool for tests. Not for game purposes

Well, that’s the same question im trying to find out.

1 Like

What are they? Aren’t they both globally accessible?

Think of it as global tables then. I’m not an Roblox expert lol

You said they’re different scopes, what are they?

Because they’re different global tables. Think of it as your local table thing but global

Backwards compatibility. Deprecated implies that it will be removed. _G is a language global and still present in old projects. I don’t imagine it’s far-fetched to call it legacy but it won’t get deprecated. You just shouldn’t use it in new work because ModuleScripts better accomplish any and all use cases you can think of for the global table as well as designs that the global table doesn’t solve.

2 Likes

No? I thought it implied that it will be crossed out in the auto-suggestions and shouldn’t be used in the future, which is exactly what you’re saying.

I don’t really understand this reply.

You asked me why they still exist and why they aren’t deprecated – it’s for backwards compatibility purposes. Deprecation itself implies the intent to remove the API in the future, the strikethrough is only a visual representation that it is deprecated. It’s not deprecated so it does not appear striked.

Deprecation isn’t related to autosuggestions.

2 Likes

From Roblox Globals:

From Lua Globals:

However, beware!!! shared != _G

If it serves the same purpose, why are there two?

I think you’re confusing those with getfenv.

Nah, I just wanted to show that neither table indexes the other.

I’m confused, why would one table index the other?

Because they represent the global environment.

Pretty obvious if nothing was put in it by any other script…?

These are default global variables, you don’t declare & initialise them yourself.

1 Like

Yes you do? For example if I say _G.amogus = 1 or shared.amogus = 1 and then print(_G.amogus) it would work but if I only did print(_G.amogus) it wouldn’t.

They share similar purposes, not the same purpose.

_G values only work on scripts that share the same execution level (level 2, 3, etc) while
shared only works for scripts with the same class (localscript, script, etc).

6 Likes

That just means you can index and assign to them, not that you are declaring them yourself (they exist from the get-go).

1 Like