Problems printing messages only once inside a loop

I made a local function that contains a loop that is always set to true, and inside that loop there is a check if “textboolvalue” is true or false. Inside the check if the value is false, there is a function for a textbutton and when clicking on this button, the “textboolvalue” will be true and will print “done” ending up infinitely printing “now is true”. But there is a problem, when the “done” is printed, instead of being once, it will print hundreds of times and only then will it go to “now is true”, what I want is for the “done” be printed only once, what do I do?

Code:

local textboolvalue = false

local function test()
while true do
wait()
if textboolvalue == false then
script.Parent.TextButton.MouseButton1Click:Connect(function()
textboolvalue = true
print(“done”)
end)
elseif textboolvalue == true then
print(“now is true”)
end
end
end

spawn(test)

Output:
image

It’s because you’re creating the MouseButton1Click event multiple times due to it being inside of a loop, I’d put it above the while true do loop so it’s only made once and thus only print done once

1 Like

well, that makes sense, thanks

1 Like