Can we fire local functions using RemoteEvent?

Hello. I was wondering can we FireClient a local function using RemoteEvent?
These are the scripts:

ModuleScript
local player = game:GetService("Players").LocalPlayer

local function test()
    print("RenderStep")
end

script.Parent.RemoteEvent:FireClient(player, test())
LocalScript
script.Parent:WaitForChild("RemoteEvent").OnClientEvent:Connect(function(test)
    game:GetService("RunService").RenderStepped:Connect(function()
        test()
    end)
end)

Any answers will be appreciated :+1:

2 Likes

You can do it, since the code there would be sent as a parameter for usage, just remove the parenthesis.

1 Like

@rakaxcvn457 I got you.

You can do that because you just share the data.

You can share anything that its a variable.

BUT THE PROBLEM IS: Dont share it using test(). Use test
Fix:

Old Script: script.Parent.RemoteEvent:FireClient(player, test())
New Script: script.Parent.RemoteEvent:FireClient(player, test)
1 Like

Oh ok. Thanks for your answer.

You meant in the script.Parent.RemoteEvent:FireClient(player, test()) line right?

parenthesis are these things (), remove them for the function, not FireClient

1 Like

Oh I see. Thanks for your answer.

No! There are restrictions, and functions are included there, they cannot be passed through RemoteEvent, it will be nil
Docs: Argument Limitations for Bindables and Remotes | Roblox Creator Documentation

2 Likes

So, is it impossible to do that?

Yes. You can make a module or pass already the result of a function

2 Likes

An example of this would be, for example, to create a module with a dictionary of functions and to pass the id of the function you want to use. EX:

-- Module

local funcs = {}

function funcs.doSomeStuff()
    -- Blah blah blah...
end

function funcs.doOtherStuff()
    -- Blah blah blah...
end

return funcs
--[[
{
  ["doSomeStuff"] = function: .....
  ["doOtherStuff"] = function: .....
}
]]
-- Server
YourEvent:FireClient(pLayer, "doSomeStuff")
-- Client
local Funcs = require(wherever.your.module.is.placed)

YourEvent.OnClientEvent:Connect(function(func)
    Funcs[func]() -- You can pass in your own parameters here...
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.