4 time communication between client and server

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to Have the client ask the server something, then the server reply, then the client reply, and then the server will send the final result.
  2. What is the issue? Include screenshots / videos if possible!
    Not sure how to do this, no similar issues
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried doing thing but It didn’t work

remote functions only accomplish half of this

1 Like

You will need to fire a remote function twice (or use 2 remote functions).
Depends how you wish to set it up.

--[[Using 2 remote functions the process would look like this]]

--client fires 1st RF to server
--server receives -> processes -> replies
--client receives -> processes -> fires 2nd RF
--server receives -> processes -> replies
--client receives -> processes

--[[Using 1 Remote function it would look like this]]

--client fires RF to server with identifier 1
--server receives -> checks identifier -> runs process 1 -> replies
--client receives -> processes -> fires RF with identifier 2
--server receives -> checks identifier -> runs process 2 -> replies
--client receives -> processes

--[[Using nested remote functions]]

--client invokes RF 1
--server receives -> processes -> invokes RF 2
--client receives -> processes -> returns RF 2
--server receives -> processes -> returns RF 1

Why not just send all the information to the server the first time so it can automatically reply with the correct result.

1 Like

how would I wait until the second one is fired

1 Like

this is a security thing, it uses a formula to send back 2 numbers sent by the server

2 Likes

would this work?

–client
function RequestChangeData(action,Variable,Value)

--the server sends 2 numbers then the client uses a formula to combine them then the server checks if the client used it correctly
	game.ReplicatedStorage.RemoteFunction:InvokeServer()
	local SecurityKey0,SecurityKey1 = game.ReplicatedStorage.RemoteFunction:Wait()
	local result = game.ReplicatedStorage.RemoteFunction:InvokeServer(action,"TutorialCompleted", false, (SecurityKey0/23 * SecurityKey1*34)*3/4 + 43)
	return result

end
RequestChangeData(1,“TutorialCompleted”,false) – delete to make it work normally.

–Server
local queuedPlayers = {}
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(Player,Action,variable,Value,Vefify)
if queuedPlayers[Player.UserId] then
if ((queuedPlayers[Player.UserId][1]/23 * queuedPlayers[Player.UserId][2]*34)*3/4 + 43) == Vefify then
if Action == 1 then
PlayerDataModule.setData(Player, variable, Player.UserId * 3213.895)
local Result = PlayerDataModule.getData(Player)

		PlayerDataModule.setData(Player, variable, Value)
		return(0)
	elseif Action == 2 then
		local Result
		Result = PlayerDataModule.getData(Player)

		return(Result)
	end

	end
else
	local key1 =math.abs(math.random(0,10000))
	local key2 = math.abs(math.random(0,10000))
	game.ReplicatedStorage.RemoteFunction:InvokeClient(Player,key1, key2)
	local tablesec = {key1,key2}
	queuedPlayers[Player.UserId] = tablesec
end

end

1 Like

That’s something @Wigglyaa could answer better than I could.

1 Like

can you ask them to answer it then?

1 Like

I’m not aware of any method to get a remote function to ‘wait’, in the way you’re asking, they have to return a value and each firing would start a separate thread.
It would also be unsafe to wait in case the client never responds, as this would leave the thread waiting indefinitely.

You can have a table to record the values set in the same scope as the listeners which will allow them to share data between the different remote function threads. You could also keep a timestamp with the data and check this to provide a timeout.

(I would have to test the code, but don’t have time right now)

how about getting a remote event to wait?

1 Like

The best way I can think of to approach your question is just to have your code handling the function yield with a task.wait loop and set a variable to the client’s reply- which the task.wait loop will check- when received.

1 Like

I had a think and came up with a third option which allows the first remote function to wait for a reply from the client, (I amended my first post).
It’s not ideal as if the client disconnects the thread could error, so you may need to wrap the RF in a pcall and handle accordingly.

Check this for more general info.