如何在此代码中实现去抖动

How can I implement a debounce in this code

我的第一个想法是在 Roblox devforum 上提问,但由于 imo 他们的录取系统真的很乱,我不妨在这里问一下。

我有一个工具可以在鼠标点击时将方块(楔子)射到鼠标指向的任何地方。它还会投射一条射线,并且方块本身会将与其接触的任何类人动物的生命值设置为 0。但我不知道如何在枪上实际实施冷却时间,所以你不能只是字面上的垃圾邮件方块杀死任何触及他们的东西。我认为在这里实施去抖动是最好的选择,但我从第一天起就一直坚持下去,我不知道如何正确地写下来

我已经尝试了访问此页面后想到的大部分内容 Roblox dev page about Debounce,还通读了开发论坛中一些有类似问题的文章,但我可以随意屏蔽垃圾邮件块做。

该工具只有两个部分(一个是句柄),一个将这些部分结合在一起的本地脚本,一个在单击时捕获鼠标位置的本地脚本,两个将信息从本地脚本传递到服务器脚本的远程事件,以及以下服务器脚本

local tool = script.Parent
local clickEvent = tool.ClickEvent
local clickEventConnection
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

--Function that creates the part with a touched listener that kills any humanoid that comes into contact with said block

local function createPart(location)
  local part = Instance.new("WedgePart")
  part.CFrame = location
  part.Parent = workspace
  part.BrickColor = BrickColor.new("Black")
  part.Touched:connect(function(hit)
    if hit.Parent then 
        local hum = hit.Parent:FindFirstChild("Humanoid")
        if hum then
            hum.Health = 0
        end
    end
end)
  game:GetService("Debris"):AddItem(part, 2)
end

--With the information on the click position of the localscript, this function creates a ray and a beam that accompanies the block, as well as executing the createpart() function on said location

local function onClick(player, clickLocation, ignore)
  createPart(clickLocation)
  local ray = Ray.new(
    tool.Handle.CFrame.p,                           
   (clickLocation.p - tool.Handle.CFrame.p).unit * 500 
) 
local hit, position, normal = workspace:FindPartOnRay(ray, player.Character, ignore)
local beam = Instance.new("Part", workspace)

        if player.Team ==  Teams["Blue Team"] then
            beam.BrickColor = BrickColor.new("Bright blue")
        elseif player.Team ==  Teams["Red Team"] then
            beam.BrickColor = BrickColor.new("Bright red")
        else
            beam.BrickColor = BrickColor.new("Ghost grey")
        end
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (tool.Handle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

        game:GetService("Debris"):AddItem(beam, 1)
end

--subscribing onclick() when equiping the weapon and unsubscribig when unequipping it
local function onEquip()
  clickEventConnection = clickEvent.OnServerEvent:connect(onClick)
end

local function onUnequip()
  clickEventConnection:disconnect()
end

tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)

我只是想制作一个 'cooldown' 这样每 3 秒就可以发射一个方块。事实上,您可以随心所欲地发送垃圾邮件

消除点击的一种简单方法是使用变量来决定是否快速退出函数。

您可以修改 onClick 函数,使其在冷却时间仍然存在时不执行:

-- make a cooldown tracker
local isGunOnCooldown = false
local cooldownTime = 3.0 --seconds

local function onClick(player, clickLocation, ignore)
    -- debounce any spammed clicks
    if isGunOnCooldown then
        return
    end

    -- put the gun on cooldown
    isGunOnCooldown = true

    -- fire a bullet
    createPart(clickLocation)
    local ray = Ray.new(
        tool.Handle.CFrame.p,                           
        (clickLocation.p - tool.Handle.CFrame.p).unit * 500)
    local hit, position, normal = workspace:FindPartOnRay(ray, player.Character, ignore)
    local beam = Instance.new("Part", workspace)

    if player.Team ==  Teams["Blue Team"] then
        beam.BrickColor = BrickColor.new("Bright blue")
    elseif player.Team ==  Teams["Red Team"] then
        beam.BrickColor = BrickColor.new("Bright red")
    else
        beam.BrickColor = BrickColor.new("Ghost grey")
    end
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (tool.Handle.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

    game:GetService("Debris"):AddItem(beam, 1)

    -- start the gun's cooldown and reset it
    spawn(function()
        wait(cooldown)
        isGunOnCooldown = false
    end)
end