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

How to add a proper, consistant delay to this laser gun script?

Asked by 5 years ago

I want to include a properly working delay to this laser script. I used script.Disabled, but it seems to have a few inconsistantcies. Any ideas?

mouse.Button1Down:connect(function()
        tool.Ammo.Value = tool.Ammo.Value - 1
        tool.Handle.Fire:Play()
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 2048)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
        local beam = Instance.new("Part", workspace)
        beam.BrickColor = tool.NeonLight.BrickColor
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false
        local distance = (tool.Handle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.1, 0.1, distance)
        beam.CFrame = CFrame.new(tool.Barrel.CFrame.p, position) * CFrame.Angles(math.rad(math.random(-SpreadAngle,SpreadAngle)), math.rad(math.random(-SpreadAngle,SpreadAngle)), 0) * CFrame.new(0, 0, -distance/2)
        game:GetService("Debris"):AddItem(beam, 0.1)
0
What do you mean Delay? You mean for it to wait a rate of fire before shooting again? Stephenthefox 94 — 5y
0
If you're talking about firerate, simply add a wait() at the end. Make the time that it waits (60/xx), with xx being the rate of fire of the weapon. dunkmaster452 1 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hi KingPig,

You just need to use a debounce. This is how a debounce works:

local deb = false;
local del = 2; -- This is the delay it will have.

function do_it()
    if not deb then
        deb = true;
        print("Can do it.");
        wait(del);
        deb = false;
    end
end

mouse.Button1Down:Connect(do_it);

Well, I hope I helped and have a nice day/night.

Thanks,

Best regards,

~~ KingLoneCat

Ad

Answer this question