Client input with functions

While a function is running, I want the function to wait for a clients input, and that input will then influence the output of the function. To keep things simple, I wrote an example. How would I fix my code below?

--a: some variable
--b: options for client input
--c: client input variable

--In Script
local remoteEvent = Instance.new("RemoteEvent")
local remoteFunction = Instance.new("RemoteFunction")

local function example(a, b)
	local c = nil
    remoteEvent:FireClient(b) 
	remoteFunction.OnServerInvoke = function(c)
        print(c) --This works!
		return c
	end
	print(a + c) 
end
--In local script
remoteEvent:OnClientEvent:Connect(function()
	--gets client input from options b
	remoteFunction:InvokeServer(c)
end)

Obviously at first I can just use a remote function separately from the function to get client input, and then put it into example() as a third parameter, but for my special case, I need to specifically get client input for the function.

What I do have working is the ability to get c in the onserverinvoke function. I’m not so sure about return c working though.

I also have another problem, print(a+c) doesn’t wait for onserverinvoke. Since the client takes a while to input, the function is too fast and skips right to print(a+c).

Any fixes or alternatives to what I can do? Really just looking for the waitforonserverinvoke() thing I don’t know about. :slight_smile:

A remote function returns a variable, and doesn’t necessarily work how you are putting it. The best thing to do would be this (I’m not sure if something like this works, but it should):

--a: some variable
--b: options for client input
--c: client input variable

--In Script
local remoteEvent = Instance.new("RemoteEvent")
local remoteFunction = Instance.new("RemoteFunction")

local function example(a, b)
	remoteEvent:FireClient(b) 
	local plr, c = remoteEvent.OnServerEvent:Wait()
	print(a + c) 
end
--In local script
remoteEvent:OnClientEvent:Connect(function()
	--gets client input from options b
	remoteEvent:FireServer(c)
end)

Actually, I take all of this back, instead, you should use a remote function.

--a: some variable
--b: options for client input
--c: client input variable

--In Script
local remoteEvent = Instance.new("RemoteEvent")
local remoteFunction = Instance.new("RemoteFunction")

local function example(a, b)
	local c = remoteFunction:InvokeClient(b)
	print(a + c) 
end
--In local script
remoteFunction.OnClientInvoke = function()
	return c
end)

Are you sure? I’ve heard people saying InvokeClient is insecure/buggy.

InvokeClient is just as insecure as FireServer.

Okay, I’ll get back to you on this brb