Delaying remote function's returning without yielding code

Hello, I’ll get straight to the point.

I want to, as the title says, delay the time it takes the remote function to return a value without yielding the existing code. How would I go about this and is it even possible?

My remote function is fired from the server and waits for a response from the client.

Example code:

local event -- my event

event.OnClientEvent:Connect(function(timeToWait))
	-- use something to wait that doesn't yield the code
	return true
end)

Feel free to ask for details. Thanks for your help!

Where is the code located that you don’t want the yield to affect?

local event -- my event

event.OnClientEvent:Connect(function(timeToWait)
	spawn(function()
		task.wait(timeToWait)
		return true
	end)
	
	--// do whatever you want here

end)

That wouldn’t actually return anything though because you’re returning the spawn instead of the RemoteFunction.

I have a while loop below the remote function that controls all the functions within the script.

Below and outside of? That’s fine then, a yield in the function won’t cause the loop to yield.

I am confused, it actually works?
When I tried it a second ago it didn’t work lol.
Thanks man and you too @CZXPEK . Sorry for wasting your time.

1 Like