Basically, as of right now, I’m checking whenever a text change happens, and then showing, text before waiting a few seconds to remove it, however, if I click it a lot of times, then the text only shows up for a split second (as time has passed since the first call)
Is there a better way to do this, so if this is already running (waiting) and it’s called again, to break the original call and only use the newest call
local RunService = game:GetService("RunService");
local t = tick();
local change = false;
LockedLabel:GetPropertyChangedSignal('Text'):Connect(function()
t = tick() + 3;
change = true;
end)
RunService.Heartbeat:Connect(function()
if change then
if tick() >= t then
LockedLabel.Text = '';
change = false;
end
end
end)
local RunService = game:GetService("RunService");
local heartbeat, latestChange
LockedLabel:GetPropertyChangedSignal('Text'):Connect(function()
latestChange = tick()
if not heartbeat then
heartbeat = RunService.Heartbeat:Connect(function()
if tick()-latestChange >= 3 then
LockedLabel.Text = ''
heartbeat:Disconnect()
heartbeat=nil
end
end)
end
end)
personally, this is what i use, it’s just like using delay(t, callback)
local threads = {}
local function nwait(t, f)
threads[#threads + 1] = tick()
while tick() - threads[#threads] < t do service.RunService.Stepped:wait() end
spawn(f)
table.remove(threads, threads[#threads])
end
I am confused. Why not just use a very simple and efficient debounce?
local LockedLabelDebounce = false
LockedLabel:GetPropertyChangedSignal('Text'):Connect(function()
if not LockedLabelDebounce then
LockedLabelDebounce = true
wait(3)
LockedLabel.Text = ''
LockedLabelDebounce = false
end
end)
Because if you call the function and then after 2 seconds you call it again you want it to clear the text 3 seconds after the second call now, not 3 seconds after the first one. So the ‘countdown’ has to be updated and your function does not allow the second call to update the countdown.
Try this, no loops or RenderStepped/Heartbeat event handlers.
local mostRecentCall = nil
LockedLabel:GetPropertyChangedSignal('Text'):Connect(function()
local t = tick()
mostRecentCall = t
wait(3)
if t == mostRecentCall then
LockedLabel.Text = ''
end
end)