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
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
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
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
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.
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)
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.