While true do isn't even doing it's job

So I have a few lines of code:

	while script.Parent.Text ~= tostring(ans) do
		task.wait()
		print("wait")
	end

All the hierarchy is right, but the loop never cuts out even if the textbox is equal to answer. I’ve checked it by using print statements everywhere.

What could be done?

well It looks like it should stop looping, but you could add the if statement into the loop and add “break”

It complains about script.Parent.Text being nil, yet the waits still print.

Even if the correct answer is in there it doesn’t cut out, I’ve tried contenttext as well

Double check that the text are actually equal by the following:

Make sure ans is what you want it to be.
Use print statements to print the Text value and ans simultaneously.

This is just to better understand your issue. Let me know your discoveries after trying this.

1 Like

Very odd, answer is always 0

here’s the whole code now then:

while true do
	num1 = tonumber(script.Parent.Parent.num1.Text)
	num2 = tonumber(script.Parent.Parent.num2.Text)

	if script.Parent.Parent.Symbol.Text == "+" then
		symnum = 1
	elseif  script.Parent.Parent.Symbol.Text == "-" then
		symnum = 2
	elseif  script.Parent.Parent.Symbol.Text == "×" then
		symnum = 3
	else
		symnum = 4
	end

	if symnum == 1 then
		ans = num1 + num2
		script.Parent.Parent.Symbol.Text = "+"
	elseif symnum == 2 then
		ans = num1 - num2
		script.Parent.Parent.Symbol.Text = "-"
	elseif symnum == 3 then
		ans = num1 * num2
		script.Parent.Parent.Symbol.Text = "×"
	elseif symnum == 4 then
		ans = num1 / num2
		script.Parent.Parent.Symbol.Text = "÷"
	end

	print("ANSWER: "..tostring(ans))

	while script.Parent.ContentText ~= tostring(ans) do
		task.wait()
		print("ANSWER: "..tostring(ans))
		print(tostring(script.Parent.ContentText))
	end

	print("gg")
	task.wait()
end

The code that puts the numbers in:

function change()
	print("im awake")
	local ans = 0
	local num1 = math.random(1,10)
	local num2 = math.random(1,10)
	print("random ints worked")
	local symnum = math.random(1,3)
	script.Parent.num1.Text = tostring(num1)
	script.Parent.num2.Text = tostring(num2)
	print("random answer worked")
	if symnum == 1 then
		ans = num1 + num2
		script.Parent.Symbol.Text = "+"
	elseif symnum == 2 then
		ans = num1 - num2
		script.Parent.Symbol.Text = "-"
	elseif symnum == 3 then
		ans = num1 * num2
		script.Parent.Symbol.Text = "×"
	elseif symnum == 4 then
		ans = num1 / num2
		script.Parent.Symbol.Text = "÷"
	end
end

script.Parent:GetPropertyChangedSignal("Enabled"):Connect(change)
if script.Parent.Text and script.Parent.Text ~= "" or script.Parent.Text ~= " " then
    while script.Parent.Text ~= tostring(ans) do
        task.wait()
        print("wait")

        if script.Parent.Text == tostring(ans) then
            break
        end
    end
end

Make sure to always add an if statement to while loops (if possible), so if it returns false, it won’t error.

In addition, make sure to break the loop if the condition is true, so it’s not infinite! Hopefully this helps, if you have any questions regarding this thread, please let me know! :cloud:

1 Like

Try this:

while true do
	if script.Parent.Text ~= tostring(ans) then
		print("TEXT IS NOT ANS, : " .. tostring(script.Parent.Text) .. " TO " tostring(ans))
		task.wait()
		print("wait")
	else
		task.wait()
		print("TEXT IS ANS, : " .. tostring(script.Parent.Text) .. " TO " tostring(ans))
	end
end

Welp, after trying a few things the entire script refuses to work now, even though it’s not disabled.

What on earth

Possibly because there’s a while loop within a while loop? The script may be getting exhausted, try adding a break after the first loop is finished.

There really is no reason as to why it would be breaking now and not before… :thinking:


Side note: To stop a while loop, use break statements, they will stop the while loop. If there is important code at the bottom that must run, but there’s a while loop interrupting it, make sure to break the while loop!

1 Like

You are starting a loop inside of a loop, which stops the outer loop. Therefore, the ans variable is not changing because the loop is yielded.

You need to embed the second while loop in task.spawn() (code threading). Let me know if you need explanation.

1 Like

It gets deeper, I changed the outside while loop to a function controlled by attributes, and now it’s saying that my attribute value of 2 is nil…

@KristinaMoment For clarification, here is an updated code using the code you provided:

while true do
	num1 = tonumber(script.Parent.Parent.num1.Text)
	num2 = tonumber(script.Parent.Parent.num2.Text)

	if script.Parent.Parent.Symbol.Text == "+" then
		symnum = 1
	elseif  script.Parent.Parent.Symbol.Text == "-" then
		symnum = 2
	elseif  script.Parent.Parent.Symbol.Text == "×" then
		symnum = 3
	else
		symnum = 4
	end

	if symnum == 1 then
		ans = num1 + num2
		script.Parent.Parent.Symbol.Text = "+"
	elseif symnum == 2 then
		ans = num1 - num2
		script.Parent.Parent.Symbol.Text = "-"
	elseif symnum == 3 then
		ans = num1 * num2
		script.Parent.Parent.Symbol.Text = "×"
	elseif symnum == 4 then
		ans = num1 / num2
		script.Parent.Parent.Symbol.Text = "÷"
	end

	print("ANSWER: "..tostring(ans))
        task.spawn(function()
	while script.Parent.Text ~= tostring(ans) do
		task.wait()
		print("ANSWER: "..tostring(ans))
		print(tostring(script.Parent.Text))
	end
        end)
	print("gg")
	task.wait()
end
1 Like
while true do
	num1 = tonumber(script.Parent.Parent.num1.Text)
	num2 = tonumber(script.Parent.Parent.num2.Text)

	if script.Parent.Parent.Symbol.Text == "+" then
		symnum = 1
	elseif  script.Parent.Parent.Symbol.Text == "-" then
		symnum = 2
	elseif  script.Parent.Parent.Symbol.Text == "×" then
		symnum = 3
	else
		symnum = 4
	end

	if symnum == 1 then
		ans = num1 + num2
		script.Parent.Parent.Symbol.Text = "+"
	elseif symnum == 2 then
		ans = num1 - num2
		script.Parent.Parent.Symbol.Text = "-"
	elseif symnum == 3 then
		ans = num1 * num2
		script.Parent.Parent.Symbol.Text = "×"
	elseif symnum == 4 then
		ans = num1 / num2
		script.Parent.Parent.Symbol.Text = "÷"
	end

	print("ANSWER: "..tostring(and))

task.spawn(function()
    while script.Parent.Text ~= tostring(ans) do
        task.wait()
        print("ANSWER: "..tostring(and))

		print(tostring(script.Parent.Text))
	end
end)

	print("gg")
	task.wait()
end

** Reformatted for readability.

This still does nothing, I have no idea why now. No errors being thrown

edit: found minor spelling errors (changed and to ans)
edit 2: actually it still does nothing

Rewrite the code & cut down the number of lines, utilize task.spawn() or coroutine.create(), (for the second while loop embedded inside the outer while loop), and debugging statements.

Those are about the only things you can do, otherwise we have nothing for you.

2 Likes

This happens because this code was ran right before you input the correct answer in the textbox, which results this while loop to be false and moves on the script execution. Even if you input the correct answer, the while loop would still not run because script execution already dealt with it and won’t come back for it.

For this, you have to repetitively check the value by using RunService.Heartbeat which gets fired indefinitely, making a great tool to replace the while loop in this case.

I managed to fix it with repeat wait until loops…

how…

edit: hold up, my fridge proximity prompt only opens the gui once what…

function click()
	while game.Lighting.plr.Value == nil do
		task.wait()
	end
	plr = game.Lighting.plr.Value
	for r,x in pairs(game.Players:GetChildren()) do
		if x.Name == tostring(plr) then
			plr = x
		end
	end
	if plr:WaitForChild("PlayerGui").Math.Enabled == false then
		plr:WaitForChild("PlayerGui").Math.Enabled = true
	end
	script.Disabled = true
end

script.Parent.ProximityPrompt.PromptButtonHoldEnded:Connect(click)

edit 2: also, it’s 03:17 and I’m not sleeping until this is fixed + i have datastore2

Repeat loops actually completely blew past me, but I guess if it works well, it’s a solution! I hope to see your game/project on the front page one day!

Have an amazing day/night, Cloud.

1 Like

Nice to hear, but I’m gonna present this anyways since it’s probably overall better to do it like this.

local Read = Read
local tNum1 = Read.Parent.num1
local tNum2 = Read.Parent.num2
local tSymbol = Read.Parent.Symbol

local function Start(Answer)
	local Symbol = tSymbol.Text
	local Sym_ID = 4 -- Erase the need for any further comparisons by simply making the else the default.
	local Answer = nil
	local Num1,Num2 = tonumber(tNum1.Text),tonumber(tNum2.Text)
	if Symbol == "+" then
		Sym_ID = 1
	elseif  Symbol == "-" then
		Sym_ID = 2
	elseif  Symbol == "×" then
		Sym_ID = 3
	end

	if Sym_ID == 1 then
		Answer = Num1 + Num2
		Symbol = "+"
	elseif Sym_ID == 2 then
		Answer = Num1 - Num2
		Symbol = "-"
	elseif Sym_ID == 3 then
		Answer = Num1 * Num2
		Symbol = "×"
	elseif Sym_ID == 4 then
		Answer = Num1 / Num2
		Symbol = "÷"
	end

	print(string.format("[DEBUG] The answer is: %0.3f", Answer))

	local Input = nil
	local Solved = false
	Input = Read:GetPropertyChangedSignal("Text"):Connect(function()
		if Read.Text == tostring(Answer) then
			print(string.format("ANSWER: %0.3f", Answer))
			Solved = true
			Input:Disconnect()
		end
	end)
	repeat task.wait(1/4) until Solved
	print("GG! Solved!")
	return
end

-- Now you can call Start() whenever you wish,
-- for example, upon Num1 or Num2 changing.
-- That'd ensure that Num1 and Num2 are always going to be something.

As for why this is better, the only busy part of it is only actually checking every so often, and is quietly waiting in large increments in the background. And by using an event we’re ensuring the user has actually put anything into the TextBox.

As for your issue with the proximity prompt, that’s because you’re disabling the script (and therefore garbage collecting its connections) at the end of your click function.

1 Like

Client-server boundary issue. You’re likely enabling the ScreenGui on the server and later disabling it from the client, as such the change won’t replicate to the server and from the server’s perspective the ScreenGui is still enabled even though it has been disabled from the client’s side.

1 Like