Stopping the last fired event after the new event fired

I will try my best to explain what I am asking.

Value:GetPropertyChangedSignal("Value"):Connect(function()
--a loop here
end)

so now the question is, how do I entirely stop whatever is running (in this case its a loop) in the last event after a new one is fired?

The easiest way is probably to just have a single loop outside of the event, and have the event just change some variables that the loop uses.

Otherwise you can have each loop given an “ID” and change that when an event triggers. This way might have issues where the first loop still is finishing up when the second loop starts, though, if you’re not careful. But the general idea is like

local counter = 0

Event:Connect(function()
  counter = counter + 1 -- stops other loops
  local myCounter = counter
  while counter == myCounter do
    print("running loop", myCounter)
    task.wait(1)
  end
  print("loop", myCounter, "interrupted")
end
1 Like

Thanks for the idea, this will greatly help aid me in future scripting :smile: