Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Prevent people from firing too fast?

Asked by
PhaZeRO 30
8 years ago

Where can I add a debounce to this gun script? This is the raycasting laser gun from the wiki.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    print("Gun equipped")

    mouse.Button1Down:connect(function()        
        print("Mouse pressed")

        local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit * 150)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Bright yellow")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

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

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

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(17)
            end
        end
    end)
end)
0
Use a debounce/cooldown xuefei123 214 — 8y

1 answer

Log in to vote
1
Answered by
lucas4114 607 Moderation Voter
8 years ago
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local reloadtime = 1 --Change this to be what you want to time the player has to wait to shoot after he shot, in seconds.

local debounce = false

tool.Equipped:connect(function(mouse)
    print("Gun equipped")
        mouse.Button1Down:connect(function()    
        if debounce == false then
            debounce = true    
                print("Mouse pressed")

                local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit * 150)
                local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

                local beam = Instance.new("Part", workspace)
                beam.BrickColor = BrickColor.new("Bright yellow")
                beam.FormFactor = "Custom"
                beam.Material = "Neon"
                beam.Transparency = 0.25
                beam.Anchored = true
                beam.Locked = true
                beam.CanCollide = false

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

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

                if part then
                    local humanoid = part.Parent:FindFirstChild("Humanoid")

                    if not humanoid then
                        humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
                    end

                    if humanoid then
                        humanoid:TakeDamage(17)
                    end
                end
            wait(reloadtime)
            debounce = false
        end
    end)
end)
Ad

Answer this question