【问题标题】:Roblox script running multiple times?Roblox 脚本多次运行?
【发布时间】:2016-03-26 22:47:17
【问题描述】:

我需要一点帮助。基本上我有这个代码:

local plyIsEntered = false

function onTouched(hit)

plyIsEntered = true

if not plyIsEntered then

end

if plyIsEntered then

    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    local ply = humanoid.Parent
    if humanoid ~= nil then

        print("Hit")
        local playerName = hit.Parent.Name
        print(playerName)
        local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter")

        local scanLaser = Instance.new("Part", game.Workspace)

        scanLaser.Position = laserEmitter.Position
        scanLaser.Name = "ScanLaser"
        scanLaser.Size = Vector3.new(1,1,1)
        local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser)
        scanLaserMesh.Name = "Cone mesh"
        scanLaserMesh.MeshType = ""
        plyIsEntered = false


        end

    end



end

script.Parent.Touched:connect(onTouched)

现在我正在检查玩家是否触摸了一个盒子,它没有碰撞并且是不可见的;当他们这样做时,我想创建一个激光来扫描他们并打开一扇门。我遇到的问题是当我走进触发框时,它会创建 8 或 9 个方块。其中一个块是我正在应用网格的块。

我需要做的是确保它只运行一次并且不会创建超过 1 个积木。希望有人可以帮助我!

【问题讨论】:

  • 出于某种奇怪的原因,它不允许我编辑...您需要修复代码格式。另外不要忘记 roblox 有自己的 Scripters 论坛,可以帮助解决这些问题。除了建议,.Touched 会触发很多,所以你应该看看 debounce。 wiki.roblox.com/index.php?title=去抖

标签: lua roblox


【解决方案1】:

我认为要解决此问题,您需要添加去抖动功能。

你看,Part的touch事件实际上触发了很多次,所以如果它在事件内部你的代码会执行多次。

为解决此问题,我们使用了去抖动功能,这意味着如果您的部件在同一时间范围内接触过多,您的代码将不会执行。这是一个例子:

local debounce = false

part.Touched:connect(function()
   if debounce == false then
     debounce = true
     --Your code goes here.
     wait(1)--Wait one second until you'll be able to execute the code again.
     debounce = false
   end
end)

了解更多关于去抖动的信息:http://wiki.roblox.com/index.php?title=Debounce

【讨论】:

    猜你喜欢
    • 2022-10-16
    • 2021-04-02
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 2017-01-07
    • 2013-11-25
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多