How To Handle an Unresponsive RF's InvokeClient?

I am using a remote function to prompt a player to press the letter E on their keyboard. This remote function is encapsulated by a coroutine.wrap() to make it asynchronous and the return value which is a boolean tells the script whether or not the player pressed e. However, what if the player leaves or dies, and the clientinvoke is never responded to? Would this cause a memory leak? How can I handle this case?

local success = false
coroutine.wrap(function()
    success = RF:InvokeClient(plr)
    -- what if the player never responds to this invokeclient() what then?
end)()

if success then
    -- do whatever
end

Try using a pcall. It’s essentially the same thing you’re doing.

Not exactly. I won’t get an error from the RemoteFunction. Rather, it will pause at the line for infinity because the player never responded to the RemoteFunction. The reason I’m using coroutines is so the rest of the script can keep running while the script waits for the player’s response.

If I were to use pcall, the RemoteFunction invoke would freeze the entire script until the player responded(and the player might never respond!) and is pointless because the RemoteFunction won’t error anyways.

You should just consider using a remote event to tell the client an order, then handling the prompt queuing on the client. This way you can ease the burden you’re putting on the server by expecting this cross-response to happen, while also keeping input related controls on the client (where they belong).

You will have to create a response for the action sent by the client though. As convenient as it is to use remote functions for this, it’s more of a bad practice to be polling in the first place.

-- server gives an order
script.NeedsToPressE:FireClient(player)

script.NeedsToPressE.OnServerEvent:Connect(function(player)
	print(player,'pressed E! Hurray!')
end)




-- client processes it
function ProcessE()
	local eWasPressed
	
	
	-- at the very end we tell them we pressed E!
	if eWasPressed then
		script.NeedsToPressE:FireServer()
	end
end

script.NeedsTOPressE.OnClientEvent:Connect(function()
	-- do your prompt stuff
	ProcessE()	
end)
1 Like

Thank you. I remember watching a Crazyman32 video and he went on a rant about how you should never use InvokeClient for security reasons, as the client could send back anything. I thought in this case it wouldn’t matter because it is a simple request for a player to press E. However, it polls like you said and it complicates things further. I will take your advice

1 Like