为什么Roblox Lua中的这个碰撞不起作用?

我最近向我的Roblox游戏中添加了一些代码。

local detector = script.Parent -- 存储对检测器的引用。

function onTouch(part) -- 当触碰部件时运行的函数。
    print('商店已打开')
    game.StarterGui.ScreenGui.Shop_Background.Visible = true
    game.StarterGui.ScreenGui.Shop.Visible = true

end

detector.Touched:connect(onTouch) -- 将函数连接到事件的行。

目前,当我加载到游戏中时,它通常会打印商店已打开大约17次。GUI打开并阻塞屏幕。当我踩在检测器上时,它也会打印“商店已打开”。如果我进入资源管理器并将可见性设置为false,则框将取消选中,但GUI仍然存在。我有办法修复这个问题吗?

点赞
用户12839543
用户12839543
local detector = script.Parent -- 存储探测器的引用。
local debounce = false
function onTouch(part) -- 当物体被触碰时执行的函数。
    if not debounce then
        debounce = true
        print('Shop Opened')
        game.StarterGui.ScreenGui.Shop_Background.Visible = true
        game.StarterGui.ScreenGui.Shop.Visible = true
    end
    wait(2) -- 下一次执行 if 语句的时间间隔
    debounce = false
end

detector.Touched:connect(onTouch)

你应该添加 debounce 来限制函数执行的次数。
2020-03-12 17:49:05