如何在此代码中实现去抖

时间:2019-04-22 16:34:34

标签: lua roblox

我的第一个想法是向Roblox devforum提问,但是由于imo他们得到了一个非常混乱的录取系统,所以我不妨在这里提出。

我有一个工具,可以在单击时将鼠标指向的位置射出一个方块(楔形)。它还会投射光线,并且该方块本身会将与之接触的任何类人生物的生命值设为0。但是我不知道如何对枪实施冷却,因此您不能只是字面意义上会杀死垃圾邮件的方块任何触动他们的事物。我认为在这里实施防抖是最好的选择,但是从第一天开始我就一直坚持下去,我也不知道如何正确记录下来

在访问此页面Roblox dev page about Debounce之后,我已经尝试了大多数我想过的事情,还在开发论坛中通读了一些有类似问题的文章,但是无论我做什么,我都可以阻止垃圾邮件。

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

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)

我只是想进行一次“冷却”,因此可以每3秒发射一次方块。照原样,您可以随意发送垃圾邮件

1 个答案:

答案 0 :(得分:0)

消除点击次数的一种简单方法是使用变量来确定是否要从函数中快速转义。

您可以修改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