Lua 我在Roblox中的脚本工作得很好,但一旦我添加了一个去盎司,它仍然工作得很好,但只是有时候?

Lua 我在Roblox中的脚本工作得很好,但一旦我添加了一个去盎司,它仍然工作得很好,但只是有时候?,lua,roblox,debouncing,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(

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

你的问题是范围界定。解盎司将始终设置为true,但有时仅会设置回false。如果不进行更改,该函数显然将永远不会再次运行。您将希望避免使用类似于
的行,例如如果debounce==false,则debounce=true
,因为它们使您更难注意到在相同的范围内没有更改debounce

固定代码:

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)

请注意,这两条语句都改变了去盎司对齐的值。

您的问题在于范围界定。解盎司将始终设置为true,但有时仅会设置回false。如果不进行更改,该函数显然将永远不会再次运行。您将希望避免使用类似于
的行,例如如果debounce==false,则debounce=true
,因为它们使您更难注意到在相同的范围内没有更改debounce

固定代码:

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)
请注意,这两条语句都更改了“去盎司对齐”的值