While loop breaking for I,V loop

I don’t have much time so I apologize if this is sloppy, but basically I have a working local script, but when I add this code the script basically stops working but has no error messages.

code im adding (ik its commented but if I uncomment it the whole thing stops working)

--[[	while wait(0) do

		if door:FindFirstChild("Hint") then  -- ERROR
				if door.Hint.Value == true then
					script.Parent.Frame.Buttons.Hint.Visible = true
					script.Parent.Frame["Hint/Answer"].Text = "Answer"
					if door.Answer.Value == true then
						script.Parent.Frame["Hint/Answer"].Visible = false
						script.Parent.Frame.Buttons.Hint.Text = tostring(data.code)
					else
						
						script.Parent.Frame.Buttons.Hint.Text = "Hint: " .. Hints[tonumber(tostring(door):sub(5,#door.Name))]
					end
				else
					script.Parent.Frame["Hint/Answer"].Text = "Hint"
				end 
			end 
		end ]]
Any help is appreciated

Is there an error message outputted? If not, your script is probably just getting stuck at your while loop because it will yield the script at that point forever and not reach other parts of it. There are several ways to fix this but the recommended way would be to use the task.spawn() function.

task.spawn(function()
    while true do
        task.wait() -- wait() is deprecated so use task.wait() instead (they have similar behavior)
        if door:FindFirstChild("Hint") then
            if door.Hint.Value == true then
				script.Parent.Frame.Buttons.Hint.Visible = true
				script.Parent.Frame["Hint/Answer"].Text = "Answer"
				if door.Answer.Value == true then
					script.Parent.Frame["Hint/Answer"].Visible = false
					script.Parent.Frame.Buttons.Hint.Text = tostring(data.code)
				else
					script.Parent.Frame.Buttons.Hint.Text = "Hint: " .. Hints[tonumber(tostring(door):sub(5,#door.Name))]
				end
			else
				script.Parent.Frame["Hint/Answer"].Text = "Hint"
		    end 
        end
    end
end)

This function will spawn your loop in a “coroutine” or “new thread”, which means it will run simultaneously with the rest of your code. Coroutines are helpful when you have code that will yield such as a while loop.

Useful resources:
Task Library (task.spawn, task.wait) - Roblox Creator Hub
While Loops - Roblox Creator Hub

1 Like

There is no error outputted, let me try your method

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