Code after :Wait() firing twice?!

So I have this snippet of code that comes after a :Wait() statement, but after the wait is passed, it fires twice.

if i == 1 then
					opMenu.Option1.Text = charTexts[next][op].Message
					opMenu.Option1.Visible = true
					opMenu.Visible = true
					task.spawn(function()
						warn("Waiting...")
						opMenu.Option1.Activated:Wait()
						warn("Unwaiting!")
						if changed == true then return end
						changed = false
						chosen = op
						pressed = true
						warn("They chose "..op)
						warn(" ")
					end)
				else

(Simple Terms: The message “Unwaiting” will be warned twice once the activated statement has been, well, activated)

Information: This is inside of an ipairs loop which is inside of an if statement. This statement is located within a while loop inside a function.

I’ve been trying to figure out why this happens, previously the :Wait() stopped the double printing but now it’s happening again.

For more information, I have a module full of messages and outcomes that the player can choose, such as when the thing says “Hey there!” the player can choose between “Yo” and “Bye”. This code is for displaying the player’s options and detecting when they are clicked.

If you need more details, please inform me. Thanks, @Pish85 signing out.

Also sorry for bad indentation.

1 Like

The script could be running twice, try placing print statements in multiple places.

You mean the function? If so, it’s not that. I checked.

Is anything else double printing? or just that

Strangely, no, only that is double printing. I put prints at the start of function, start of loop, start of if statement, literally everywhere that indented

That’s odd, maybe try re-opening studio see if that changes anything. If that doesn’t work, im stumped on solutions :d

Nope. Didn’t work. Thanks for helping, though.

how often is the while loop going?

its possible it’s going a second time due to the entire thing running twice before the event gets fired

The while loop runs as soon as

  1. The NPC finishes talking plus a 2 second wait
  2. The player clicks an “option”

Also, I tested if the while loop fired twice with print statements, and it doesn’t. The main area of trouble is the code snippet.

Maybe you should use the task.wait function.

if none of the other prints/warns go twice, there’s no reason it would happen, as code runs linearly, and if "Unwaiting!" prints twice, at the very least "Waiting..." would fire

It’s designed so that the code fires after the button is pressed. If you didn’t read the whole post, the code says OpMenu.Option1.Activated:Wait(), showing that I want to fire the code after the button is pressed, not a set time.

If that’s not what you meant, I always use task.wait. I just said wait cuz it’s shorter.

The problem is, “Waiting” prints once, but “Unwaiting” prints twice. I’m assuming that it’s because of events inside events or so, but that was previously fixed by replacing :Connect with :Wait

it’s not within an event though, :Wait() only yields until the event is next fired, it has no effect after that, as it automatically disconnects it

although if you do connect something within another event, and you only need to use it once, use :Once instead of :Connect

For the first part, what I’m saying is I previously had this same issue, so I replaced my Connect statement with a Wait. However, now that the issue is back, I’m worried I did something wrong regarding that chance.

For the second part, I’ll try using Once and report back.

The issue is likely that Activated:Wait() is being triggered twice—check if the button is being clicked multiple times or if the function is being spawned more than once.

no, that wouldn’t be it, since :Wait won’t get jumped to when the event fires, it only waits there until the event gets fired

:Wait is closer to :Once then it is to :Connect

I think you should use opMenu.Option1.Activated:Connect() instead of Wait(), and disconnect the connection after the first activation to prevent multiple triggers.

Oh! I see. Your right, i was confused therefore.