Passing tables through bindable events

Hey everyone. I’m trying to pass a table through a bindable event, but when doing so, the script receiving the table actually receives a copy of the original table, not the table itself:
ohno

Is there a way I can use a single table in multiple scripts, so that a key change made in one script replicates to others? Thanks.

2 Likes

You can use a ModuleScript that stores the table, and whenever a script requires the module, it will receive the same table:

local t = {}

return t
1 Like

That’s an interesting idea, but I’m not sure if that would work for what I’m doing. I created an object-generating modulescript that uses metatables to create a new object table when necessary, and I need to be able to use these new object tables between scripts. I could maybe create a new module script on the fly for each new object instance and set its source to the new object instance but that seems kinda dirty.

Oh, maybe I could use _G. I’ll try that.

If you are using an OO approach, you can still use ModuleScripts to do the job, you would just have to store the objects somewhere, like in the original module:

local module = {
    Storage = {},
    Methods = {}
}

module.Methods.__index = module.Methods

function module.new(key)
    local object = setmetatable({
        -- attributes here
    },module.Methods)
    local key = key or {}
    module.Storage[key] = object
    return object, key
end

function module.get(key)
    return module.Storage[key]
end

return module

_G works too, it just may not be as clean I guess…

6 Likes

Oh okay, that makes sense, thanks.

1 Like

I just tried this and I was able to pass a table through. Encode it with JSON before you send it, decode afterwards.

local HS = game:GetService(“HttpService”)
local list = {“hi”,“bananas”,“fred”}
list = HS:JSONEncode(list)

game.ReplicatedStorage.Eventt.Event:Connect(function(listtt)
print(listtt)
print(HS:JSONDecode(listtt))
end)

2 Likes

Just tried that, but it doesn’t send the exact same copy/instance of the table. Here’s the code I used:

local http = game:GetService("HttpService")
game.ReplicatedStorage.Event.Event:Connect(function(Table)
    	print("Encoded table received: ")
    	print(Table)
    	Table = http:JSONDecode(Table)
    	print("Decoded table received: ")
    	print(Table)
    end)

    local TableToPass = {}
    wait(1)
    print("Table sending: ")
    print(TableToPass)
    print("Encoded table sending: ")
    local encodedTable = http:JSONEncode(TableToPass)
    print(encodedTable)

    game.ReplicatedStorage.Event:Fire(encodedTable)

Output:
ohno2

Thanks for the help though

1 Like

Must be because it’s a table full of Objects instead of just regular values I guess. Oh well, modules would probably be your best bet then.

1 Like