using the table as a variable to execute a function in a server script via a remote event:
example:
local script
RemoteEvent:FireServer(table)
server script
RemoteEvent.OnServerEvent:Connect(function(plr,table) for i = 1,#table do print(table[i]) end)
I just wondered if this would actually work and if it doesn't is there a way I could get it to work?
Yes, you can pass a table through a RemoteEvent
, but there are some complexities to this.
If you try to pass a dictionary with an array, the dictionary portion will be lost. Note that this only applies if they are in the same table at the same level, so you can nest an array in a dictionary or vice versa just fine.
This is okay to send through a remote:
local PlayerData = { "hi", { test = "wow", anotherKey = "we're in the dictionary" } }
This is also okay:
local PlayerData = { key = "hi", another = "value", tryAnArray = { 1, 2, "stringHere", "moreNesting", { 6, 7 }, { tryingDictionary = 4, again = 6 } } }
But this would lose the dictionary:
local PlayerData = { hi = 4, 2 }
And if you try to pass a table with any key that is not a string, and if the table is not an array, it will error.
For example:
local PlayerIdToName = { [1] = "ROBLOX", [59299229] = "Avigant" }
Though the 1
key will go inside the array, the 59299229
key won't be placed inside the array, so if you try to pass this table through a remote, it will error. This is important to keep in mind.
Instances that are values in the table will be set to nil
if they do not exist on the computer you are firing the remote to. For example, if you send an object in game.ServerStorage
to the client, it will be nil
.
Functions cannot be passed through remotes. Userdata can. Metatables will be lost.