Returning from a function without the return part being in it?

Basically I want to return from a function without the returning part being in it, I can’t really explain it well but here is an example of what I want

function A()
while task.wait(1) do
print("A")
end
end

-- Here I'm trying to return from A function to stop the task.wait(1) loop
return ???

Here is my actual script.

function module.ElevatorTimer()
	while task.wait(1) do
		if MaxAmount >= Amount then
			Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Visible = false
			Elevator.ElevatorUIPart.ElevatorUI.CurrentPlayers.CurrentPlayers.Text  = "Teleporting.."  
			return 
		end

		Amount -= 1
		Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Text = Amount
		Lobby.Sounds.Time:Play()
	end
end

module.ElevatorTimer()

if #PlayersInElevator == 0 then
	Elevator.ElevatorUIPart.ElevatorUI.CurrentPlayers.CurrentPlayers.Text = Elevator.Players.Value .. "/" .. Elevator.MaxPlayers.Value
	Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Visible = false
	Amount = 10
	MaxAmount = 1
	-- trying to return here
      end
  end
end)`

Any help is appreciated.

You can use coroutines if you want to stop a function

local x = coroutine.create(function()
	while task.wait() do
		print("hello")
	end
end)

coroutine.resume(x)
task.wait(3)
coroutine.close(x) --function stops here

if you meant you want to break the while loop then use break

Seems like it’s a solution I will try it out now.

@RatiusRat Basically I don’t want the if statement to be in the loop as it will have to wait 1 second.

it doesn’t work and it won’t because it doesn’t update the table.

			Elevator.Players:GetPropertyChangedSignal("Value"):Connect(function()
				if Elevator.Players.Value <= 1 then

					Elevator.ElevatorUIPart.ElevatorUI.CurrentPlayers.CurrentPlayers.Visible = true
					Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Visible = true
					Elevator.ElevatorUIPart.ElevatorUI.CurrentPlayers.CurrentPlayers.Text = Elevator.Players.Value .. "/" .. Elevator.MaxPlayers.Value
					local MaxAmount = 1
					local Amount = 10 
				
					local ElevatorTimer = coroutine.create(function()
						while task.wait(1) do
							if MaxAmount >= Amount then
								Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Visible = false
								Elevator.ElevatorUIPart.ElevatorUI.CurrentPlayers.CurrentPlayers.Text  = "Teleporting.."  
								return 
							end

							Amount -= 1
							Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Text = Amount
							Lobby.Sounds.Time:Play()
						end
					end)
	

					coroutine.resume(ElevatorTimer)
					
					
					if #PlayersInElevator == 0 then
						Elevator.ElevatorUIPart.ElevatorUI.CurrentPlayers.CurrentPlayers.Text = Elevator.Players.Value .. "/" .. Elevator.MaxPlayers.Value
						Elevator.ElevatorUIPart.ElevatorUI.Timer.TimerText.Visible = false
						Amount = 10
						MaxAmount = 1
					end
				end
			end)
			
			
			game.Players.PlayerRemoving:Connect(function(Player)
				if table.find(PlayersInElevator, Player.UserId) then
					table.remove(PlayersInElevator, table.find(PlayersInElevator, Player.UserId))
					if Elevator.ElevatorUIPart.ElevatorUI.Players:FindFirstChild(Player.UserId) then
						Elevator.Players.Value -= 1
						Elevator.ElevatorUIPart.ElevatorUI.Players[Player.UserId]:Destroy()
					end
				end
			end)

			RemoteEvents.LeaveElevator.OnServerEvent:Connect(function(Player)
				if Player.Stats.InRoom.Value == true and table.find(PlayersInElevator, Player.UserId) then
					Player.Stats.InRoom.Value = false
					table.remove(PlayersInElevator, table.find(PlayersInElevator, Player.UserId))
					print(#PlayersInElevator)
				end
			end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.