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

Global functions with tables?

Asked by 8 years ago

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

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
8 years ago

_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()
0
Okay, thanks for the help! User#9949 0 — 8y
Ad

Answer this question