Search code examples
inputwhile-loopluaroblox

How do I prevent ONE "while" process to run twice at the same time?


The problem's that when I double click quickly, the weapon runs one "while" process twice at the same time (which makes the weapon fire undeterminately as long as the left click is held down), thus making the weapon shot faster than expected. It also, somehow, ignores the InputEnded function (which states that the while proccess yields when the left click is released), which would obviously prevent this "while" stacking as you can't double click without releasing the left mouse button first, obviously. I link the proccesses of the code below.

conn = UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        humanoid.WalkSpeed -= shoting
        toolDebounce = true -- the boolean that must be true for the while process to run
        while toolDebounce do
            local startPosition = tool.Part.Position
            local endPosition = mouse.Hit.Position

            remoteEvent:FireServer(tool, startPosition, endPosition)

            task.wait (debounceTime)            
        end
    end
end)


conn2 = UIS.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        toolDebounce = false -- when the boolean in question is set to false, the while proccess yields. this only works if the interval between clicks is, aproximately, 0.4 seconds long
        humanoid.WalkSpeed += shoting
    end
end)

Any help is aprecciated and will be tried.

I tried adding an "if not" clause before the InputBegan function, which would, in theory, prevent the gun from firing twice, as the toolDebounce boolean would still be true. However, it did not work, as the gun can still run the while function needed to fire twice at the same time though the boolean is still true, ignoring the if not clause which I stated before, somehow.


Solution

  • local debounce=false
    conn = UIS.InputBegan:Connect(function(input, gameProcessedEvent)
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            if debounce then return end
            debounce=true
            humanoid.WalkSpeed -= shoting
            toolDebounce = true -- the boolean that must be true for the while process to run
            while toolDebounce do
                local startPosition = tool.Part.Position
                local endPosition = mouse.Hit.Position
    
                remoteEvent:FireServer(tool, startPosition, endPosition)
    
                task.wait (debounceTime)            
            end
            humanoid.WalkSpeed += shoting
            debounce=false
        end
    end)
    
    
    conn2 = UIS.InputEnded:Connect(function(input, gameProcessedEvent)
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            toolDebounce = false -- when the boolean in question is set to false, the while proccess yields. this only works if the interval between clicks is, aproximately, 0.4 seconds long
            
        end
    end)