Issue with `While wait() do` loops

  1. I’m trying to make a script that when a value is equal to [X], it changes a UI’s text.

  2. Even if the event is fired and the value is [X], it doesn’t do anything. It’s as if the script never gets to the loop.

  3. Making it only run when the UI is enabled, but it still didn’t work.


-- This is client sided.
local start = game.ReplicatedStorage.StartQuest --Find our remote event
local player = game.Players.LocalPlayer

	local gui = player.PlayerGui --Find the players personal GUI
	local QuestSpeechUI = gui:WaitForChild("QuestSpeechUI") --Find our quest dialogue
	local yesButton = QuestSpeechUI:WaitForChild("Frame"):WaitForChild("Talk"):WaitForChild("Yes") --Find our yes button
	local noButton = QuestSpeechUI:WaitForChild("Frame"):WaitForChild("Talk"):WaitForChild("No") --Find our no button
	local dialogueUI = QuestSpeechUI:WaitForChild("Frame"):WaitForChild("Talk")
	local questRefused = player:WaitForChild("questRefused")
	QuestSpeechUI.Enabled = true --Make our UI visible
      while wait() do
		if questRefused.Value == false then --When our yes button is clicked
			dialogueUI.Text = "Thanks for helping me out! If you complete my task, I'll reward you handsomely." --You can change this to whatever you want and it will show this text if they accept quest
			yesButton.Visible = false
			noButton.Visible = false
		elseif questRefused.Value == true then

Maybe your WaitForChilds are never completing. Are you sure all those names are right?

Use the lua debugger to step through your code line by line and see where it has trouble.

Also, it’s a bad idea to have a never-ending while loop in an event like this. What if the event gets called twice?

explains why not to use wait and alternatives

1 Like

Try using questRefused.Changed to check when the values is changed instead of always checking

They complete correctly, as the UI gets enabled and no errors appear if I watch the line.

I will, but I don’t think it will fix my problem.

Edit: Using .Changed didn’t fix my problem, it still failed to do anything when it was equal to true.

I’m just going to mark this as the solution, as I managed to fix it on my own. Thanks to anyone who tried to help.