如何防止“while”进程同时运行?

问题描述 投票:0回答:1

问题是,当我快速双击时,武器会同时运行两次“while”过程,这使得只要按住左键,武器就会不确定地开火,从而使武器的射击速度比预期的要快。 它还以某种方式忽略了 InputEnded 函数,该函数指出当释放左键单击时, while 过程会产生,这显然会阻止此“while”堆栈,因为显然,如果不先释放鼠标左键,则无法双击。 我链接了下面代码的过程。

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)

任何帮助都值得赞赏并将尝试

我尝试在 InputBegan 函数之前添加一个“if not”子句,从理论上讲,这将防止枪两次射击,因为 toolDebounce 布尔值仍然为 true。然而,它不起作用,因为枪仍然可以运行同时开火两次所需的 while 函数,尽管布尔值仍然为 true,忽略我之前所说的 if not 子句,不知何故

input while-loop lua roblox
1个回答
0
投票

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
        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
        humanoid.WalkSpeed += shoting
    end
end)

© www.soinside.com 2019 - 2024. All rights reserved.