Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problems with _G?

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

✓ Accepted answer

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.

2 comments

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.

0

Oh, I guess I misread the last line. That's a shame, hope you can find a workaround.

This discussion is preserved as it was. New answers, comments, and votes are closed.