【问题标题】:Is there a reason why this collision isn't working for Roblox Lua?这种碰撞对 Roblox Lua 不起作用有什么原因吗?
【发布时间】:2020-06-23 22:44:41
【问题描述】:

我最近将其中一些代码添加到我的 Roblox 游戏中。

local detector = script.Parent -- Store a reference to the detector.

function onTouch(part) -- The function that runs when the part is touched.
    print('Shop Opened')
    game.StarterGui.ScreenGui.Shop_Background.Visible = true
    game.StarterGui.ScreenGui.Shop.Visible = true

end 

detector.Touched:connect(onTouch) -- The line that connects the function to the event.

目前,当我加载到游戏中时,它通常会打印 shop opened 大约 17 次。 GUI 会打开并挡住屏幕。当我踏上检测器时,它也会打印“商店已开业”。如果我进入资源管理器并使可见等于 false,则该框未选中,但 gui 仍留在那里。有什么办法可以解决这个问题吗?

【问题讨论】:

  • 确保您只执行该函数一次。通过检查它是否已被调用或在第一次运行时断开事件处理程序。我猜你多次触发了那个触摸事件。

标签: lua collision-detection roblox


【解决方案1】:
local detector = script.Parent -- Store a reference to the detector.
local debounce = false
function onTouch(part) -- The function that runs when the part is touched.
    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) -- Interval until the next execution of if statement
    debounce = false
end 

detector.Touched:connect(onTouch)

你应该添加 debounce 来限制函数运行太多次。

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 2014-02-01
    • 2016-06-17
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 2022-11-14
    相关资源
    最近更新 更多