How to wait for a function to return/complete in a while wait do loop?

Hello! So I’ve been having trouble figuring out how to get my code (down below) to work.

  1. What do I want to achieve? I want to have a while loop that, well of course loops every 15 seconds. Although inside the loop, the script can pick 1 of three task for the player to do. Each task is a function, and what I want to do is have the while loop wait for the function to finish before starting the 15 second timer again.

  2. What is the issue? The issue is that the while loop doesn’t wait for the function to complete, so the player may be in the middle of doing a task and then all of a sudden another one happens and the script breaks because I have it programmed to only support one task (function) at a time.

  3. What solutions have I tried so far? Not too many to be honest. I don’t know how to make a while loop wait or pause for a function until it is complete or has returned a message saying it’s complete.

Here is a main function of the script (the while loop I’m talking about). The RemoteEvent fires right when a player clicks the “Play” button (not that that is important, it’s just for context). As you can see, once the player task has been decided (either 1, 2, or 3), the loop will fun the corresponding task (or function) that goes with the player task number.

local function startGame()
	print("Starting Game!")
	while wait(15) do
		local playerTask = math.random(1, 3)
		if playerTask == 1 then
			print("Task: Phone")
			local result = phoneTask()
			repeat 
				wait()
				print("Waiting for player to finish task...")
			until result == "Phone Task: Complete"
			print(result)
		elseif playerTask == 2 then
			computerTask()
			print("Task: Computer")
			local result = computerTask()
			repeat 
				wait()
				print("Waiting for player to finish task...")
			until result == "Computer Task: Complete"
			print(result)
		else 
			coffeeTask()
			print("Task: Coffee")
			local result = coffeeTask()
			repeat 
				wait()
				print("Waiting for player to finish task...")
			until result == "Coffee Task: Complete"
			print(result)
		end
	end
end

startGameEvent.OnServerEvent:Connect(startGame)
1 Like
local bindableEvent = Instance.new("BindableEvent")

local function startGame()
	print("Starting Game!")
	while wait(15) do
		local playerTask = math.random(1, 3)
		if playerTask == 1 then
			print("Task: Phone")
			local result = phoneTask()
			repeat 
				wait()
				print("Waiting for player to finish task...")
			until result == "Phone Task: Complete"
			print(result)
		elseif playerTask == 2 then
			computerTask()
			print("Task: Computer")
			local result = computerTask()
			repeat 
				wait()
				print("Waiting for player to finish task...")
			until result == "Computer Task: Complete"
			print(result)
		else 
			coffeeTask()
			print("Task: Coffee")
			local result = coffeeTask()
			repeat 
				wait()
				print("Waiting for player to finish task...")
			until result == "Coffee Task: Complete"
			print(result)
		end
	end
	bindableEvent:Fire()
end

startGameEvent.OnServerEvent:Connect(startGame)
bindableEvent.Event:Wait()

You could use a ‘BindableEvent’ object.

Something similar to this could work. The functions should return something for the loop to continue.

local function task1()

end

local function task2()

end

local function task3()

end

function start()
	while wait(15) do
		local task = math.random(1, 3)
		local taskFunction = task == 1 and task1 or task == 2 and task2 or task3
		local returned
		repeat returned = taskFunction() until returned end
	end
end

I’ve tried this:

local function startGame()
	while wait(15) do
		print("Running")
		local task = math.random(1, 3)
		local taskFunction = task == 1 and phoneTask() or task == 2 and computerTask() or coffeeTask()
		local returned
		repeat returned = taskFunction() until returned 
	end
end

But this error showed up in the output:
image

The parentheses shouldn’t be included, you can see that in my code.

local taskFunction = task == 1 and phoneTask or task == 2 and computerTask or coffeeTask

That is true, although the taskFunction in the repeat loop shouldn’t have any parentheses either. This code below worked for me, so I think this is the solution.

local function startGame()
	while wait(15) do
		local playertask = math.random(1, 2)
		local taskFunction = playertask == 1 and phoneTask() or playertask == 2 and computerTask() or coffeeTask()
		local returned
		repeat returned = taskFunction until returned == "Completed"
		print("Task was completed!")
		wait(math.random(3,5))
	end
end

For anyone trying to do the same thing: Make sure to have this line of code at the end of each task’s function. For example, at the very end of my computer task function, I have this line of code:

while wait() do
		if taskComplete == true then
			return "Completed"
		end
	end

Here is a simplified version of my computer task (I also didn’t want to put the full function here because that would be useless for anyone just trying to learn how this works.):

local function computerTask()
	local taskComplete = false
    
    -- Your function code here...

    taskComplete = true -- This will end the task. 
while wait() do
		if taskComplete == true then
			return "Completed"
		end
	end
end
1 Like