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!
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
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…
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!
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.
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.
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!
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.
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.