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

Global functions with tables?

Hi, I was wondering if it was possible to put a table inside of a global function to transfer it to another script. Here is an example I tried:

Inside Main Script:

_G.test = function()
	local try = {
		Num1 = "Hey",
		Num2 = "Hello"
	}
	print(try.Num1)
end

Inside Other Script:

_G.test()

Error Workspace.Script:1: attempt to call field 'test' (a nil value)

I couldn't get it to work. Could someone help me or at least explain it?

1 answer

✓ Accepted answer

_G.test is not guaranteed to exist the moment you attempt to call it in the other script. You need to yield the thread until it exists. This can be done with a simple while loop.

-- We are going to wait until _G.test's variable type is a function instead of nil
while type(_G.test) ~= "function" do
	wait(0.03)
end
_G.test()

1 comment

0

Okay, thanks for the help!

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