How to overwrite connection to function

Hello fellow devs! I am new to scripting and struggling with code.
Is there a way to do this?

game.SomethingHappens:Connect(function(value)

	for i = 1, 30, 1 do
                task.wait(1)
		print(value)
	end
end)

-- fired with value = "Hello"
--printed "Hello" x15 times
--fires with value = "World"
--printing ONLY "World" and forget about "Hello"

2 Likes

Also i can confuse terms, so ask question if you can’t understand me.

1 Like

For your provided scenario, you should use Once instead of Connect.

1 Like

Once disconnects after fired, I need to make script that stops typing “Hello” if function was fired again so it stops it action and do the new one(typing “World”)

1 Like

Can I see what your actual code looks like? The behavior you want should be the actual behavior that’ll occur. (What you’re describing would only happen if you’re connecting events within other events).

Another question I have is: if you’re not connecting events within other events, is the event firing quickly? If so, the running functions would overlap over eachother, which could be solved by this:

local current_value = nil
game.SomethingHappens:Connect(function(value)
    current_value = value
	for i = 1, 30, 1 do
        if value ~= current_value then break end -- stop the loop if a different value was fired
        task.wait(1)
		print(value)
	end
end)
1 Like

This physically isn’t possible the way you want to do it. It’d have to be something like this

local looping = false
local lastValue

local function doLoop()
    looping = true
    ... -- Use the "lastValue" variable
    looping = false
end

bindable.Event:Connect(function(value)
    if not looping then
        doLoop()
    end

    lastValue = value
end)

Not sure what you would need this for, but here it is

Thank you, this is also a solution