1

例如:脚本在一个游戏会话中运行良好,但在另一个游戏会话中,它根本不起作用;几乎就好像脚本有某种随机机会被删除或完全忽略。如果我删除去抖动,脚本有 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)
4

1 回答 1

0

您的问题是范围界定之一。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 值的两个语句都对齐。

于 2015-10-20T00:12:36.643 回答