How do i make a new wait even everytime this example is called?

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.

1 Like
  1. connect is deprecated, use Connect
  2. For a notif system shouldn’t you be using tween? And I recall you can override tweens with a new one
2 Likes

1 answer: This was just an example, it is not supposed to actually work
2 answer: The notif for now just appears, then disappears

1 Like

I think you should look into co-routines and spawning functions I believe is the correct method.

1 Like

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.

1 Like

How would i add this to my current notif code?

Here is that:
(Im not asking you to write a whole new script, just simply combine them, or tell me how)

local frame = script.Parent
local Reason = game.ReplicatedStorage.CMDErrorSender
local Error = script.Parent.Sound
local yes = script.Parent.Sound2
local HB = script.Parent.HelpButton
local ClickSound = script.Parent.Click
local location = script.Parent.Parent.Parent.Parent:WaitForChild("invisleaderstats"):WaitForChild("NotifLocal")

Reason.OnClientEvent:Connect(function(Why, IfWorked)
	if location.Value == "BR" then
		HB.Text = "<---"
		frame.Position = UDim2.new(0.877, 0, 0.835, 0)
	elseif location.Value == "BL" then
		HB.Text = "--->"
		frame.Position = UDim2.new(0, 0, 0.835, 0)
	elseif location.Value == "M" then
		HB.Text = "^^^"
		frame.Position = UDim2.new(0.438, 0, 0.8, 0)
	
	end
	frame.Reason.Text = Why
	frame.Visible = true
	if IfWorked == true then
		frame.TextLabel.Text = "Yuh uh"
		frame.Reason.Text = Why
		yes:Play()
		if Why == "Click the arrow to open the Command instructions!" then
			HB.Visible = true
		end
	else
		Error:Play()
	end
	task.wait(5)
	HB.Visible = false
	frame.Visible = false
	frame.TextLabel.Text = "NUH UH"
end)


HB.MouseButton1Click:Connect(function()
	--resetting notification
	frame.Visible = false
	HB.Visible = false
	frame.TextLabel.Text = "NUH UH"

	--the rest
	ClickSound:Play()
end)

This code is different from the one you sent me, you only had one event in the previous one? I’m not sure how it works.

Previous one was a snipped to explain what i want more easily, this is the real code for the notif. same idea, diffrently executed tho

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.

ok ill do that later thanks mans