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

问题描述 投票:0回答:1

我的第一个想法是询问Roblox devforum,但是因为他们有一个非常混乱的录取系统,我不妨在这里问一下。

我有一个工具,可以在点击鼠标时指向一个块(楔形)。它还投射了一条射线,并且该块本身设置了与其接触的任何人形生物的健康状态为0.但是我不知道如何在枪上实际实施冷却,所以你不能只是字面上垃圾块杀死任何触动他们的东西。我认为在这里实施去抖是最好的选择,但是从第1天起我就陷入困境,我不知道如何正确地写下来

我已经尝试过在访问本页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秒就可以发射一次。按原样,您可以根据需要添加垃圾邮件

lua roblox
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.