Print arguments of a RemoteEvent

As the title states, I’m looking to see if there’s a way to get the arguments passed in an OnServerEvent or OnClientEvent for a RemoteEvent and print them to the output.

Eg if i do OnServerEvent(player, amount) and I run a piece of code to print them out, it’d return {player, amount} in a table, or something along the lines of that.

You can literally just print(player, amount), unless you’re asking about something else?

1 Like

No, I’m referring to if you’re able to do

-- script 1
RemoteEvent.OnClientEvent(player, amount)
-- etc
end

-- script 2
print(RemoteEvent.args)  -- output = {player, amount}
-- or
for i, v in pairs(RemoteEvent.args) do -- output = player amount
    print(v)
end

You can use ... to get all of the args except player in my example.

RemoteEvent.OnClientEvent:Connect(function(player, ...) 
 local args = {...}
 for i,v in ipairs(args) do
  print(i,v)
 end
end)

1 Like

You could use a bindable event or just connect another OnClientEvent event listener in your 2nd script and just print the args from there

I can see what your trying to accomplish.
Try this:

local remote = game.ReplicatedStorage.RemoteEvent

remote.OnServerEvent:Connect(function(player, amount)
     local args = {
        player,
        amount,
     }
     for i, v in pairs(args) do
         print(i, v)
     end
end)

What if I just want to print out the arguments for any RemoteEvent when the OSE or OCE function calls

Something like this?

for i,v in pairs(game:GetDescendants()) do
    if v:IsA("RemoteEvent") then 
        v:OnServerEvent:Connect(function(...)
            local args = {...}
            for i,v in ipairs(args) do
                print(i,v)
            end
        end
    end)
end

This returns

You’re attempting to do a global connection to all remote events. You can try using a collection tag. However, my solution was an example for directly connecting to a remote event, and printing the arguments passed.

How would I use a collection tag with this? I’ve never used them.

For each remote event you add, you can use CollectionService:AddTag(RemoteEvent, "tagName"). I use a plugin to manage my tags.

Can I get an example on how to use it in this case please

If you want to do it with an OSE Use this:

local remote = game.ReplicatedStorage.RemoteEvent

remote.OnServerEvent:Connect(function(player, amount)
     local args = {
        player,
        amount,
     }
     for i, v in pairs(args) do
         print(i, v)
     end
end)

for an on client event you don’t need to use a function for the player, you can just use the localplayer inside the script. Like this:

local remote = game.ReplicatedStorage.RemoteEvent
local player = game.Players.LocalPlayer

remote.OnClientEvent:Connect(function(amount)
     local args = {
        player,
        amount,
     }
     for i, v in pairs(args) do
         print(i, v)
     end
end)

This is because when using a server script. You need to call the player inside the function. But I a local script the player is already used.

2 Likes

But your given examples don’t use CollectionService?

You never mentioned “collectionservice” the code above that I provide should work.