【问题标题】:My script in Roblox works fine, but once I added a debounce, it still worked perfectly, but only sometimes?我在 Roblox 中的脚本运行良好,但是一旦我添加了去抖动,它仍然可以完美运行,但只是有时?
【发布时间】:2016-01-11 20:50:05
【问题描述】:

例如:脚本在一个游戏会话中运行良好,但在另一个游戏会话中,它根本不起作用;几乎就好像脚本有某种随机机会被删除或完全忽略。如果我删除去抖动,脚本有 100% 的机会再次工作。这里可能出了什么问题?

local radius = script.Parent
local light = radius.Parent.Light
local sound = radius.Parent.lighton

local debounce = false

radius.Touched:connect(function(hit)
    if debounce == false then debounce = true
        if game.Players:GetPlayerFromCharacter(hit.Parent) then
            light.PointLight.Brightness = 10
            light.Material = "Neon"
            sound:Play()
            wait(5.5)
            light.PointLight.Brightness = 0
            light.Material = "Plastic"
            sound:Play()
            wait(0.5)
            debounce = false
        end
    end
end)

【问题讨论】:

    标签: lua roblox debouncing


    【解决方案1】:

    您的问题是范围界定之一。 debounce 将始终设置为 true,但有时会设置回 false。如果它没有被改变,这个函数显然将永远不会再次运行。您需要避免使用像 if debounce == false then debounce = true 这样的行,因为它们会让您更难以注意到去抖在同一范围内没有改变。

    固定代码:

    local radius = script.Parent
    local light = radius.Parent.Light
    local sound = radius.Parent.lighton
    
    local debounce = false
    
    radius.Touched:connect(function(hit)
        if debounce == false then
            debounce = true
            if game.Players:GetPlayerFromCharacter(hit.Parent) then
                light.PointLight.Brightness = 10
                light.Material = "Neon"
                sound:Play()
                wait(5.5)
                light.PointLight.Brightness = 0
                light.Material = "Plastic"
                sound:Play()
                wait(0.5)
            end
            debounce = false
        end
    end)
    

    请注意,这两个语句都会更改 debounce align 的值。

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多