如何防止一个“while”进程同时运行两次?

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

提问人:martin 提问时间:9/30/2023 最后编辑:martin 更新时间:10/2/2023 访问量:55

问:

问题是,当我快速双击时,武器会同时运行两次一个“同时”进程(只要按住左键,武器就会不确定地开火),从而使武器射击速度比预期的要快。 它还以某种方式忽略了 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)

任何帮助都会被接受并尝试。

我尝试在 InputStarted 函数之前添加一个“if not”子句,从理论上讲,这将阻止枪发射两次,因为工具Debounce boolean仍然为真。但是,它没有用,因为枪仍然可以运行同时发射两次所需的 while 函数,尽管布尔值仍然为真,不知何故忽略了我之前所说的 if not 子句。

输入 while-loop lua roblox

评论

0赞 InSync 9/30/2023
请不要发布代码/错误/文本内容的图片。将它们作为文本粘贴到此处。

答:

0赞 user15611379 9/30/2023 #1

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)

评论

0赞 martin 10/1/2023
虽然堆叠问题不再出现,但现在,当我双击时,我的角色的速度(人形。WalkSpeed)不断添加“射击”值,使我速度惊人,而不仅仅是在我退出射击时添加它。我能做些什么?
0赞 user15611379 10/1/2023
移动“人形。WalkSpeed += shoting“ after while 循环