For example, lets say an event comes in. It will make a part invisible for a second, then visible. If that event comes in multiple times, the parts visisbility will glitch. How can i make a “new” wait event, so that i dont have to wait for the function to finish?
here is example code:
local event = Path.To.Event
local Part = Path.To.Part
event.Fired:connect(function()
Part.Transparency = 0
task.wait(1)
Part.Transparency = 1
end)
Thanks! i need to use this for a notification system.
You can either use a debounce, which will ignore other incoming events if it’s already waiting, but I think you’re looking for something else, something that cancels the previous event. There are two ways I can think of, the first way is rather complex so I will use the second way.
Instead of waiting, use a loop, like this:
local shouldContinue = true
local isWaiting = false
-- in event:
event.Fired:connect(function()
-- start code here
if isWaiting then shouldContinue = false end
local i = 0
task.wait()
shouldContinue = true
isWaiting = true
while i < 1 do
i += task.wait()
if not shouldContinue then return end
end
isWaiting = false
-- end code here
end)
It’s relatively simple, and it looks like it doesn’t do anything, but it should work and reset the wait.
I would use coroutines, but they can be finicky sometimes when it comes to cancelling.
Okay, I won’t touch that script, I don’t want to break anything in it. But here’s the logic behind what I’m doing. You don’t have to use coroutines.
Wherever you’re waiting, loop over and wait in intervals, until you’ve waited x amount of seconds (x being how long you want the notification to last). While you’re looping, constantly check for a variable if it’s true, and if it is continue looping.
When you want to cancel it, from wherever that is, set that same variable to false. It’s as simple as that. Make sure when you start looping the variable is true so it doesn’t never start.