Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problems with _G?

Asked by
funyun 958 Moderation Voter
8 years ago

I started making a module for short-hand ways to do things in studio that I commonly do, like import stuff from the catalog with InsertService. So, instead of

game:GetService("InsertService"):LoadAsset(125013769).Parent = workspace

I could do

require(workspace.Utilities).import(125013769, workspace)

to do the same thing. However, that's also a lot of work to type out, so I thought I could make it even shorter by making a little script to put the utilities in _G, and then I'd just do this

_G.import(125013769, workspace)

And for some reason, my script refuses to put all the functions in _G. I'm not sure what's happening. Here's the module:

local module = {}

function module.import(id, location)
    local model = game:GetService("InsertService"):LoadAsset(id)
    local thing = model:GetChildren()[1]
    thing.Parent = location
    model:Destroy()
end

return module

And here's the globalizer:

utils = require(script.Parent)

for key, value in pairs(utils) do
    _G[key] = value
end

print("Utilities globalized.")

And from there I just use the command line to actually import the sword.

1 answer

Log in to vote
1
Answered by
Lamar 45
8 years ago

I just tested your scripts and they work fine for me. What errors are you encountering on your end? The only potential error I can think of is that you're trying to use the function before _G has been populated. In which case, you should use a loop to wait until _G has been populated. This is how I would do it:

repeat wait() until next(_G) ~= nil

This bit of code waits until there's something in _G, then continues the script. This way, you can't reference _G anywhere else until it's been populated. If you try to call an index that doesn't yet exist, it obviously won't work!

Hopefully this helps, if you're experiencing a different kind of issue then let me know.

0
Apparently it won't work with the command line. I just found out that the command line has its own separate _G. Thanks anyway. funyun 958 — 8y
0
Oh, I guess I misread the last line. That's a shame, hope you can find a workaround. Lamar 45 — 8y
Ad

Answer this question