Can you yield until a remote function is true?

This is a pretty basic question, I’m just asking if it’s possible to wait until you receive anything from the remotefunction you invoke.

Example:

(Client Side)

local rem = script.Parent.RemoteFunction
rem.OnClientInvoke = function()
wait(3)
return "Pie"
end)

(Server Side)

local rem = script.Parent.RemoteFunction
local message = rem:InvokeClient(player)
repeat task.wait(1) until message == "Pie"
print("Received Message", message)

https://developer.roblox.com/en-us/api-reference/callback/RemoteFunction/OnClientInvoke

An OnClientInvoke event is fired when the RemoteFunction:InvokeClient function is called from a server script.

repeat wait() until message
print("Received Message", message)

You can call the function named “wait” with no arguments to yield until a message has been received.
Also the variable named “message” can be evaluated as a boolean expression itself, you don’t need to check if it’s a string value equal to “Pie”.

Do not do this. Do not ever use remote functions from server.

@MeteorxBG Why wouldn’t you use this? If it shouldn’t be used then why has Roblox added it as a feature?

@Limited_Unique So if on the client I wait 3 seconds then return a message will I still be able to wait on the server until the message is received?

Simple remote functions yield the code from running until a response comes from the other side (which is client here). and if you know what exploiting is, you would know that an exploiter can easily add wait(math.huge) or simply deleting the onClientInvoke function resulting in server yielding forever.

i would say instead of using remote functions from server, use 2 remote events instead.

@MeteorxBG

I could use 2 remote events but that’s no different than using a remote function, since you’ll infinitely wait until the 2nd remote event gives you the message.

I guess I’ll just not yield anything from the client since that’s basically what you’re telling me to do. Aren’t there any other ways to prevent exploiters from doing the whole wait(math.huge) though?

instead of waiting infinitely for the 2nd remote use a bool value

local responseGiven = script.BoolValue
local waitTime = 15--seconds to wait until a response(which is the 2nd remote)
--you might want to make the number a bit high as the client might be laggy, so you should get clients ping as well.
local passedTime = 0
repeat
    task.wait(1)
    passedTime += 1
until responseGiven.Value == true or passedTime > waitTime
responseGiven.Value = false
--do the rest

the basic version is this.

4 Likes

next part of the script i wrote is(thought might be confusing by itself)

local clientResponse = ReplicatedStorage:WaitForChild("ClientResponse")

local function onClientResponse(plr)
    responseGiven.Value = true
end

clientResponse.OnServerEvent:Connect(onClientResponse)

if you want to really understand it check this out:

Theres an opensource module for exactly that which OP might find useful
https://devforum.roblox.com/t/postie-an-elegant-alternative-to-remotefunctions-with-a-timeout/

1 Like

yoooo niceeee. this is what it needs to be in my opinion, the roblox one does not have a timeout. hmm but it seems to be no longer updated. ;-;

If the client is invoking to the server, it will yield the script until something is returned.

RemoteFunction:InvokeServer()

What I did eventually was use remote function from client to server, and using remote events with timeout to mimic a remote functions from server to client