如何在 Roblox Studio 中打印鼠标点击次数

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

我在 ScreenGui 中创建了一个文本按钮。我想在每次有人点击该 TextButton 时打印点击次数。我在 ScreenGui 的 LocalScript 中写了如下代码:

local UserInputService = game:GetService("UserInputService")
local clicked=0
    
while true do

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        clicked = clicked+1
        print("Clicked "..clicked)
        wait(0.5)
        
    end
end)
if clicked>4 then
    break
end

但是,每当我运行此代码并单击一次时,它会打印随机点击次数(54 次或 100 次等)。当我第二次点击时,它打印了 500 次。

谁能帮我写代码,让它只打印 TextButton 上的确切点击次数?

lua click mouse roblox
1个回答
0
投票

你有一个 TextButton,只需使用 MouseButton1Click 事件。此外,使用

while true do
只会在您添加新连接时冻结您的客户端并且没有
wait()
。而是在事件处理程序中检查
clicked
变量更新。例子:

local btn = script.Parent
local clicked = 0

local function cb() 
   -- code to call when clicked is more than 4
end

local conn; conn = btn.MouseButton1Click:Connect(function() 
    if clicked > 4 then 
        conn:Disconnect(); -- disconnect the connection so it stops recieving events
        cb() 
    else
        clicked += 1 -- increase click by 1
    end
end)
© www.soinside.com 2019 - 2024. All rights reserved.