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

Can you pass a table through the client to the server?

Asked by 6 years ago
Edited 6 years ago

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?

0
Yes this works fine? oftenz 367 — 6y
0
Well, it would work but you forgot and end on line 4 User#20388 0 — 6y

1 answer

Log in to vote
2
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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.

0
What would occur if {CFrame:unpack()} is inside the table? Unbunn_makes 48 — 6y
Ad

Answer this question