Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you send a table through a remote event?

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)


5 comments

0

You´re only firing one argument, so data is (theoretically) nil.

0

What do I need to put instead then?

0

Fire a second table to the server and data won´t be nil.

0

Can you show me an example in the answer area?

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

3 answers

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.

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!

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 :)

This discussion is preserved as it was. New answers, comments, and votes are closed.