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

How do you send a table through a remote event?

Asked by 5 years ago
Edited 5 years ago

I have code, but it doesn't work, heres my code:

Client:

local Example = {"Test"}

 script.Parent.Activated:Connect(function()

    game.ReplicatedStorage.Submit:FireServer(Example)

  end)

Server:


game.ReplicatedStorage.Submit.OnServerEvent:Connect(function(player, request, data) if request == "Example" then for i, v in pairs (data) do print(v) end end end)
0
You´re only firing one argument, so data is (theoretically) nil. DeceptiveCaster 3761 — 5y
0
What do I need to put instead then? LEWIS1063 6 — 5y
0
Fire a second table to the server and data won´t be nil. DeceptiveCaster 3761 — 5y
0
Can you show me an example in the answer area? LEWIS1063 6 — 5y
0
ClientSided Fix: Add *Example* to line 5 as first argument, or whatever it's called. `game.ReplicatedStorage.Submit:FireServer("Example", Example)` ServerSided Fix (if you didn't do the ClientSided Fix): Remove `if request == "Example" then` and replace `data` with `request` ~~~~~~~~~~~~~~~~~ for i, v in pairs (request) do print(v) end ~~~~~~~~~~~~~~~~~ thesit123 509 — 5y

3 answers

Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago

The reason this doesent work is because of line 1 in the server script. When you do :Connect(function(player, request, data), player equals the player, request equals the table, and data is nil. The first argument is the player who fired the event, the second is the first argument, and in your case, the third is nil because there is no second arg fired. To fix this, replace line 5 of the client with this:

game.ReplicatedStorage.Submit:FireServer("Example",Example)

This is because the table is one value, and the variable name isnt fired by default.

Ad
Log in to vote
0
Answered by 5 years ago

Since there is only 1 value being processed, you dont really need to put it through a table, so

local Example = "Test"

is fine.

However, when you do need to put a table through it, there are multiple ways of doing it. First, your method, the way you would receive would be:

game.ReplicatedStorage.Submit.OnServerEvent:Connect(function(player, request, data)
        if request[1] == "Example" then
            for i, v in pairs (data) do
                print(v)
            end
        end
end)

This searches through the table and finds the first value.

What I like to do is this:

local Example = {}
Example.CMD = "Example"

 script.Parent.Activated:Connect(function()

    game.ReplicatedStorage.Submit:FireServer(Example)

  end) -- Local

-- Server
game.ReplicatedStorage.Submit.OnServerEvent:Connect(function(player, request, data)
        if request.CMD == "Example" then
        for i, v in pairs (data) do
            print(v)
        end
        end
    end)

With the way I use, I can easily get table values without having to search through the number order, and easily access them.

Hope this helps!

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

What i do is this:

Client:

local re = game.ReplicatedStorage.RE
local tab = {name = "Tim", age = 18}
re:FireServer(tab)

Server:

local re = game.ReplicatedStorage.RE
re.OnServerEvent:connect(function(player,tab)
    local args = unpack({tab})
    print("Name :"..args.name)
    print("Age :"..args.age)
end)

hope this helps :)

Answer this question