Code not working (reapeat loop)

So, Basically I had an idea for a rng based afk game.
I worked on it for like 30 minutes and the code was working all good except.
It doesn’t stop calculatig, I tried many other stuff to fix it like a break function, whilte true do loops and others.

Any help? :smiley:

local inp1 = script.Parent.INPUT1
local inp2 = script.Parent.INPUT2
local goal = script.Parent.GOAL
local calc = script.Parent.CALCULATER
local ggt = script.Parent.GGTEXT
local Value = script.Parent.GGTEXT.Value
local testtimes = script.Parent.TextLabel
inp1.FocusLost:Connect(function(enter)
	if enter then
		print(inp1.Text)
	end
end)
inp2.FocusLost:Connect(function(enter)
	if enter then
		print(inp2.Text)
	end
end)
goal.FocusLost:Connect(function(enter)
	if enter then
		print(goal.Text)
	end
end)
--Here we got the main code i'm gonna talk about.
calc.MouseButton1Down:Connect(function()
	repeat
		wait(0.1)
		print("Calculating...")
		local matheee = math.random(inp1.Text, inp2.Text)
		Value.Value = Value.Value + 1
		testtimes.Text = matheee
	until matheee == goal.Text--Here is the buggy part. 
	print(Value.Value)
	ggt.BackgroundTransparency = 0
	ggt.Text = "GG! You got the goal in : "..Value.." Tries!"
end)

What are you trying to do here?
It keeps calculating because its repeat (Going forever).

Well i’m trying to make the loop try to find the goal number that the player want, and when it does so,
It will stop calculating and change the text of the Text label into GG stuff value thing and get a badge (i have no robux)

Answer = [Insert stuff here for the answer]
Guess = [Insert stuff here]
While not Guess = Answer do
[Do stuff here]
end

bru what the hell explain :confused: ?
jeffreyblocksmefromsendingmessage

In your case:
local inp1 = script.Parent.INPUT1
local inp2 = script.Parent.INPUT2
local goal = script.Parent.GOAL
local calc = script.Parent.CALCULATER
local ggt = script.Parent.GGTEXT
local Value = script.Parent.GGTEXT.Value
local testtimes = script.Parent.TextLabel
local mathee = 0
inp1.FocusLost:Connect(function(enter)
if enter then
print(inp1.Text)
end
end)
inp2.FocusLost:Connect(function(enter)
if enter then
print(inp2.Text)
end
end)
goal.FocusLost:Connect(function(enter)
if enter then
print(goal.Text)
end
end)
–Here we got the main code i’m gonna talk about.
calc.MouseButton1Down:Connect(function()
Do while not mathee == goal.Text
wait(0.1)
print(“Calculating…”)
matheee = math.random(inp1.Text, inp2.Text)
Value.Value = Value.Value + 1
testtimes.Text = matheee
End
if mathee == goal.Text then
print(Value.Value)
ggt.BackgroundTransparency = 0
ggt.Text = “GG! You got the goal in : “…Value…” Tries!”
end
end)

Edit:
I forgot to change mathee to outside the loop
:confused:

What i mean is
While not [Your answer] = [What it guesses] do
[Do your code]
end

Your answer is the answer the computer is computing
It guesses numbers until it is equal to the answer
and then put your code in the while for it to actually guess a number

the thing my friend
the mathee variable, you big genius, is inside the loop and the code can’t find it.

Define the mathee variable outside, and then you change it inside the loop.
Make sense?
Since its local inside it cannot access it outside the loop
move it so it defines it outside
and then change it through the loop

yeah it does
hello poggers i hate c cap

local inp1 = script.Parent.INPUT1
local inp2 = script.Parent.INPUT2
local goal = script.Parent.GOAL
local calc = script.Parent.CALCULATER
local ggt = script.Parent.GGTEXT
local Value = script.Parent.GGTEXT.Value
local testtimes = script.Parent.TextLabel
local matheee

local inputs = {inp1, inp2, goal}

for _, input in ipairs(inputs) do
	input.FocusLost:Connect(function(enter)
		if enter then
			print(input.Text)
		end
	end)
end

calc.MouseButton1Down:Connect(function()	
	repeat
		task.wait(0.1)
		Value.Value += 1
		matheee = math.random(inp1.Text, inp2.Text)
		testtimes.Text = matheee
	until matheee == tonumber(goal.Text)
	
	ggt.BackgroundTransparency = 0
	ggt.Text = "GG! You got the goal in : "..Value.." Tries!"
end)

The breaking condition of the repeat until loop was comparing a string value with a number value (which would always evaluate to false) meaning that the loop would never terminate.

To solve that, use tonumber(), which converts strings to numbers
just hope u didn’t put a number or symbol in it…
and it wont error

tonumber() won’t error even if a value is passed which can’t be converted into a number, it’ll just return nil.

Well it returns in error in actually lua, I think…

UPDATE: neither of you guys’ codes worked, so what i had to do is simply change the values in the line that is buggy from mathee to the testtimes value. Thanks for your time in here helping anyways! :smiley:

Both of the provided scripts work, I believe you likely just had a logic error (script not performing as you intended), it might be better if you use more descriptive variables in future so that we get a better picture of what’s being attempted.

1 Like