Calling OnServerEvent/OnServerInvoke function later in the script?

Is it possible to call an OnServerEvent/OnServerInvoke function later in the script?

For example,

game.ReplicatedStorage.CharSelected.OnServerInvoke = function HandleCharacter(player, char)
game.ReplicatedStorage.CharSelected.OnServerEvent:Connect(function HandleCharacter(player, char)


adding the HandleCharacter name gives this line an error, but without the name, there doesn’t appear to be a way to call the function again later. I need to call it again because if I make a different function later with the same code as the first one, it won’t have the parameter that I passed from my Local Script.

Try this instead:


The problem with that is I can’t use the parameter that I passed from the localscript, it shows as an unknown global.

Alright, try this instead:


That doesn’t work either unfortunately. Parameter is nil according to the output.

Lambdas and assignments can’t take a name.

RemoteFunction_CharSelected.OnServerInvoke = function(player, ...)

RemoteEvent_CharSelected.OnServerEvent:Connect(function (player, ...)

Other than that, yes you can define OnServerInvoke or connect to OnServerEvent at any time during run time. That doesn’t seem to be your exact issue though? I’m not too sure I understand based on the context given in the OP, sounds confusing. What’s your actual issue here?

1 Like

I already have a predefined function HandleCharacter() in my server script, but what I need is to get a variable from a LocalScript to the ServerScript. I was using the RemoteEvent/Function to pass the variable as a parameter for the function. The HandleCharacter function is called multiple times throughout the script, but only the OnServerEvent/Invoke has the parameter needed. So basically, lets say I pass a variable TextButton.Value from LocalScript to ServerScript. The value is defined as such

--local script

--server script
RemoteEvent.OnServerEvent:Connect(function (player, TextButtonValue)


Once I call OnServerEvent and define a function, there’s no way for me to call said function again or for me to access the parameter TextButtonValue later in the script.

Hopefully that made sense lol

I think so? Let’s see if I have this right:

So you just need the TextButtonValue’s value later? TextButtonValue is only accessible in the scope of the lambda. If you want to access it later outside that connection then you’ll need to store it in a place where you can pull it out of, such as a cache table based in a ModuleScript.

Crude example:

-- Create a table containing players and the values they fire. Make sure to
-- clean this up when the player leaves to avoid a memory leak.
local PLAYER_VALUES = {}

-- The remote should receive the argument. It can then optionally also
-- call the function.
RemoteEvent.OnServerEvent:Connect(function (player, value)
    PLAYER_VALUES[player.UserId] = value

    -- Don't know how this is called, but assuming you call it with a player.

-- HandleCharacter will pull from the cache table regardless of who invoked.
local function HandleCharacter(player)
    local fetchedValue = PLAYER_VALUES[player.UserId]

    -- etc

That’s exactly it. I just fed you the long version lol. Let me look over that for a while and I’ll get back to you. And by a while, I mean… you’ll probably hear from me tomorrow.