Hey! I have buttons in my game and when you are on top of the button, the button starts pressing automatically, without having to move or jump, it works, but when I leave and go to the button very fast it seems to create another loop and don’t remove the old loop, which make you press faster, and I can’t find a solution for this problem.
To see, if that’s happening I printed the time between each press,
Before:
After:
Here’s the code:
local testTick = tick()
function ButtonService:Press()
print(tick() - testTick)
testTick = tick()
end
function ButtonService:SetUp(button)
button.Hitbox.Touched:Connect(function(hit)
if hit.Name ~= "HumanoidRootPart" then return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
if not self.debounce[player] then self.debounce[player] = {} end
self.debounce[player][button] = true
task.spawn(function()
while self.debounce[player] and self.debounce[player][button] do
self:Press()
task.wait(1)
end
end)
end
end
end)
button.Hitbox.TouchEnded:Connect(function(hit)
if hit.Name ~= "HumanoidRootPart" then return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
if self.debounce[player] then
self.debounce[player][button] = nil
end
end
end
end)
end