Making sure, RemoteFunction.OnServerInvoke pass the player argument throw a module

Today, I was trying to create a simple, framework that gets module function and places as RemoteFunction.OnServerInvoke, but I ran into a problem, where it wouldn’t send in the first argument as a player.

Here the example,

Server Script

local Module = {}

function Module:Test(Player)
	print(Player)
end


game.ReplicatedStorage.RemoteFunction.OnServerInvoke = Module:Test()

Client

game.ReplicatedStorage.RemoteFunction:InvokeServer()
1 Like

OnServerInvoke would be defined as the return value of calling Module:Test() which is nil. Use dot syntax to both define the function and index it to be assigned to OnServerInvoke.

local Module = {}

function Module.Test(player)
    print(player)
end

game:GetService("ReplicatedStorage").RemoteFunction.OnServerInvoke = Module.Test

If there’s any special reason why you need colon syntax and it’s not just because you want to make your code “neater” or conventional with Roblox objects, then you need to use a lambda to reroute the arguments. The self parameter will be in conflict with the implicitly passed player argument, so self will become the player and not the table from which the method is called from.

local Module = {}

function Module:Test(player)
    print(player)
end

game:GetService("ReplicatedStorage").RemoteFunction.OnServerInvoke = function(...)
    Module:Test(...)
end
4 Likes