While loop keeps running even when using break

The problem I am facing is that when I fire this function, the loop continues, and so if I fire it several times, the while loop is running 7 times other (so itll print 1 7 times at once)

The function should only run when the active is different to Updating (so it wouldn’t run if active was true and it was already running as true)

local function update(player, mood, active)
	if active == Updating then return end
	
	Updating = active
	
	if not Updating then
		UpdatingMood = nil
	end

		while wait(3) do
			if Updating then
				print(1)
				if User.Moods[mood] < 100 then
					UpdatingMood = mood
					User.Moods[mood] = User.Moods[mood] + 5
					getMood(player)
				end
			else
				break
			end
		end
end
2 Likes

Updating is set to Active and the if Updating then is testing for true or false I think

1 Like

So when active is false, it should break the loop, which it does (I put a print before the break and it printed the break) but when I when fire the function again it will print 1 twice, and then break twice, and then I keep repeating that until it’s just running the loop continously

I think you might want to try
if Updating ~= active then
instead of
if not Updating then
and
if Updating == active then
instead of
if Updating then

That doesn’t make sense to do that? Updating is either true or false and is set at the top of the script,

So if active is true, updating is true. So Updating == active would be no different to if Updating

1 Like

ok, I see now.
So adding a print in, of the variable being used before each if, does not give you any more information?

1 Like

Can you please not use while wait() do? Colbert already have told many people to not use it. This is the proper way to write it:

while Updating do
	-- wait(3) -- avoiding the case of the loop being run after Updating turns off within yield

	print(1)
	if User.Moods[mood] < 100 then
		UpdatingMood = mood
		User.Moods[mood] = User.Moods[mood] + 5
		getMood(player)
	end

	wait(3)
end
1 Like

This still prints 1 multiple times in 3 seconds.

If I fire the function once, then yes, it will print 1 once every 3 seconds. But when I fire the function as active == false (which should shut down anything running inside the function) and then fire again with active being true, it then prints 1 twice in 3 seconds

1 Like

Question: What is UpdatingMood?

You can add a if statement before the loop in order to prevent running the loop more than once and is Updating in a global scope?

1 Like

UpdatingMood is used in another function which decreases the players mood stats. So it picks a random mood to decrease, if that random mood happens to be the mood that’s also Updating (UpdatingMood) then it picks again, as I don’t want it to decrease that mood while it’s being increased.

What would the if statement check for??

And Updating is only used inside the function

1 Like

just do while updating do it will auto break for u

1 Like

That’s what I currently got

Still getting the same problem tho

1 Like

where are you changing active

cuz if its in another script bad things happen like updating will never change because the active in the parameter is just a reference

1 Like

Oh ok, cause I’m using a RemoteEvent to call it
UpdateMood.OnServerEvent:Connect(update)

And then from the client it’s
UpdateMood:FireServer(true, 'Energy')

How can I get around this then??

1 Like

Diagnosis


You are using .OnServerEvent incorrectly. You flipped the parameters.

1 Like

Wdym???

I havent shared the code, but, yes, the parameters are switched around, but I also switched them around in the function. This was so I didnt always have to have a mood parameter (as setting active to false did not require a mood)

1 Like

mood is a boolean, while active is 'Energy', a string. Always write them in correct order or else the script fails.

1 Like

Ye I forgot to mention that I changed it :grimacing:

The problem isn’t with these values as firing it is

UpdateMood:FireServer(true, 'Energy')

And the function is

local function update(player, active, mood)

end
1 Like

So if I understand correctly, you are trying to stop the while loop with the Updating variable, but the next time it’s set to true the loop runs again?

What I don’t understand is why didn’t you make the Updating variable the condition rather than wait(3)? That way you wouldn’t have to use break.

Anyways, in order to make sure that the boolean is the issue, try sending a table as the active variable instead of true, (send false normally). We will use the fact that {} ~= {} for identifying the different instances of your while loop.
If that method works, you can change it to some better system (storing coroutines in a table maybe?)


Btw

I will get a little off topic here, but I noticed that you usually don’t mark your threads as solved, even after receiving a bunch of answers that are valid solutions. If you found an answer which fixes your problem or contributes to fixing it, you should mark it as a solution as a pay off for people’s will to help you. And if you solved the issue yourself, you might consider replying to your own thread saying how you fixed it, and accepting your own reply as a solution, so that other people who stumble upon your thread while having the same problem can know how they can fix it.

2 Likes

I have changed to while Updating do, mentioned in this post

But I still get the same problem

1 Like