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

How to make a gun automatic instead of single fire?

Asked by
Galicate 106
6 years ago

I have the fire rate i just dont know how to make it so that it fire while a player holds down the fire button, making it shoot multiple bullets.

local Player = game.Players.LocalPlayer
local FireRate = 0.1
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent:WaitForChild("Ammo")
tool.Equipped:connect(function(mouse)
print("Tool equipped!")
holdanim = player.Character.Humanoid:LoadAnimation(script.Parent.HoldAnim)
reloadanim = player.Character.Humanoid:LoadAnimation(script.Parent.ReloadAnim)

    mouse.Button1Down:connect(function()
        if Ammo.Value >= 1 then
        print("Mouse pressed!")
        Ammo.Value = Ammo.Value - 1
        print(Ammo.Value)
        script.Parent.Handle.Fire:Play()
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

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

        local distance = (tool.Main.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, 0.1)

        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(math.random(10,30))

            end
        end
        end
        end)
wait(FireRate)
end)

local Player = game.Players.LocalPlayer
local action_reload = false
Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(Key)
    if(Key:lower() == "r") and Ammo.Value < 7 and action_reload == false then
        action_reload = true
        script.Parent.Handle.Reload:Play()
        reloadanim.Looped = false
        reloadanim:Play()
        wait(2)
        Ammo.Value = 7
        print(Ammo.Value)
        action_reload = false
    end
end)

function onEquipped()
    holdanim:Play()
end

script.Parent.Equipped:connect(onEquipped())
mouse = Player:GetMouse()
action_aim = false
mouse.Button1Down:connect(function()
    if action_aim == false and action_reload == false then
        action_aim = true
print("trying to aim")
       repeat wait()
    Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = script.Parent.AimPart.CFrame
print("aiming")
    end
end)

1 answer

Log in to vote
0
Answered by
Jellyfosh 125
6 years ago

One way to do it is by using Coroutines . I'm unsure if there's any "correct" way to do this but it's been what works for me. Basically a Coroutine can run loops without interrupting the rest of the script, so you can repeat what lines you need to while listening for when the player is done shooting as well. Here's a sort of skeleton model of what you could do.

local tool = script.Parent
player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character

local mouseDown = false
local debounce = false

tool.Equipped:connect(function(mouse)
local function fire()
    local looper = coroutine.create(function() --Sets up coroutine function, won't run without being resumed. 
        while mouseDown == true and debounce == false do
        debounce = true
        --What to do when the player's mouse is down should go here
        wait(.1) --How much time it takes between shots
        debounce = false
        end
    end)
    coroutine.resume(looper) --Whenever the 'fire' function is called the coroutine will resume
end

    mouse.Button1Down:connect(function()
            mouseDown = true
            fire()
        end)

    mouse.Button1Up:connect(function()
        mouseDown = false
    end)

    tool.Unequipped:connect(function()
        mouseDown = false
    end)
end)
Ad

Answer this question