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

How would I go about adding a delay between clicking for a tool in my game?

Asked by 3 years ago
local Debris = game:GetService("Debris")

local Blaster = script.Parent
local Tip = Blaster.Tip

local Player = game.Players.LocalPlayer
local Character = Player.Character

local GunDamage = 30

Blaster.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        wait (.2)
        local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
        local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)

        local LaserBeam = Instance.new("Part", game.Workspace)
        LaserBeam.BrickColor = BrickColor.new("Bright green")
        LaserBeam.FormFactor = "Custom"
        LaserBeam.Material = "Neon"
        LaserBeam.Transparency = 0.25
        LaserBeam.Anchored = true
        LaserBeam.CanCollide = false

        local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
        LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
        LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)

        Debris:AddItem(LaserBeam, 0.1)

        if HitPart then
            local HitHumanoid = HitPart.Parent:FindFirstChild("Monster")

            if not HitHumanoid then
                HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Monster")
            end

            if HitHumanoid then
                HitHumanoid:TakeDamage(GunDamage)

            end
        end
    end)
end)

Pardon the mess that is the code being used, i'm still quite new to scripting and have been for the most part trying to cobble together code from various sources. I've been trying to make a blaster for an upcoming project of mine. I've gotten the projectile working (albeit towards the cursor rather than the player's direction) and even have an animation which plays, hence the "wait (.2)" of line 13. That being said, I've so far been unable to figure out how to properly stagger the input of Button1 to allow for the animation to fully play out before the player is able to fire once more. Does anyone have any suggestions on how I might rectify this?

1 answer

Log in to vote
0
Answered by
ud2v3cf 35
3 years ago

You could create what's called a "debounce." Create a debounce variable outside of the clicking function. The code will ONLY run when debounce is FALSE. When the condition is true, set debounce to true, and after a set amount of time using delay, you set it back to false. Example:

local debounce = false

tool.Activated:Connect(function()
    if dobounce then return end

    debounce = true
    delay(DELAY_TIME, function() debounce = false end)

    -- code here
end)
0
It certainly is pushing me in the right direction, I think I will need to futz around with the debounce mechanic a bit more to get a proper hang of it. Thanks for the response. SirDouglasTheScholar 2 — 3y
Ad

Answer this question