我在Roblox中的脚本工作得很好,但是一旦我添加了去抖动,它仍然可以完美地运行,但有时只是?

时间:2015-10-13 22:50:09

标签: lua roblox debouncing

例如:脚本在一个游戏会话中工作正常,但在另一个游戏会话中,它根本不起作用;几乎就好像有一些随机的机会删除或完全忽略脚本。如果我删除去抖动,那么脚本有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)

1 个答案:

答案 0 :(得分:0)

你的问题是范围界定。去抖将始终设置为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的值。