How to return data (with RemoteFunction) without stopping whole script?

Hello!

So, I am now very happy since I made my first Codelock door with GUI.

Not very important problem is that I don’t know how would I send data back to local script and then, the door would open.

This is because, currently the script first opens the door (few “animations” with for i loop) and then, the script returns data which closes GUI. How would I return data first and then open the door?

Thank you!

1 Like

It would help if you could paste your code here, but your post actually solves it for yourself:

  1. Client inputs code and sends to RemoteFunction
  2. Server checks code against known code and returns [true/false] + Data to Client
  3. Client opens door if Local, or Server opens door

If you don’t want the script itself to yield, which I don’t think is your question but I’ll answer anyway, you can do this:

  • Server has RemoteEvent which listens for discrete event at which a Player has inputed a code to open the door
  • If codes are the same it can fire a RemoteEvent which all clients listen to, returning the data needed, and not yielding the server code

The whole purpose of a remote function is to yield your code until it has the data, this is how functions (not just remote functions) work

If you want to get something without it yielding, send a call through a RemoteEvent then send it back using the same remote event

1 Like

So, this is one part from my script. I want this thing "Success", true, false to put in before the door opens, not after that:

opened = true
			thisDoor1Touch.Sound:Play()
			thisDoor1Touch.Parent.Status.BrickColor = BrickColor.new("Lime green")
			thisDoor1Touch.CanCollide = false
			for i = 0.7,1,0.01 do
				thisDoor1Touch.Transparency = i
				wait()
			end
			wait(1)
			thisDoor1Touch.Parent.Status.BrickColor = BrickColor.new("Black")
			thisDoor1Touch.CanCollide = true
			for i = 1,0.7,-0.01 do
			thisDoor1Touch.Transparency = i
			wait()
			end
			opened = false
			
			return "Success", true, false

To solve this, you can use the spawn(foo) function. It will execute foo like as if it’s on a different script. So if you wrap the animation code for the door in spawn, it will run “separately” and allow for it to be “skipped” immediately to the return statement.

Here’s the fixed code:

spawn(function()
	opened = true
	thisDoor1Touch.Sound:Play()
	thisDoor1Touch.Parent.Status.BrickColor = BrickColor.new("Lime green")
	thisDoor1Touch.CanCollide = false
	for i = 0.7,1,0.01 do
		thisDoor1Touch.Transparency = i
		wait()
	end
	wait(1)
	thisDoor1Touch.Parent.Status.BrickColor = BrickColor.new("Black")
	thisDoor1Touch.CanCollide = true
	for i = 1,0.7,-0.01 do
	thisDoor1Touch.Transparency = i
	wait()
	end
	opened = false
end)

return "Success", true, false

Thank you :slight_smile:!
It’s working.