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

Why is the gun still shooting when Reloading?

Asked by
Jephi 42
7 years ago
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local ammo = 8
local isReload = false

tool.Equipped:connect(function(mouse)
    print("Pistol Equipped")

    mouse.Button1Down:connect(function()
        print("Mouse Clicked")
        ammo = ammo - 1     

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


        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Gray")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.75
        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, 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(30)
            end

            --reloading
            if ammo > 0 then
                print("Ammo")
                isReload = false
            else
                tool.Name = "Reloading"
                isReload = true
                wait(2)
                tool.Name = "Pistol"
                ammo = 8
                print("Reloaded")
                isReload = false
            end 
            --done reloading
        end
    end)
end)

The gun continues to shoot when it reloading for two seconds. How do I make it stop shooting while it's reloading? I've already tried a few things.

0
"if isReload then" at the top and then an else right after it TheHospitalDev 1134 — 7y

1 answer

Log in to vote
1
Answered by
AZDev 590 Moderation Voter
7 years ago

--You are not checking if you are reloading in your code. You need to use an if statement to check if reloading when the player clicks.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local ammo = 8
local isReload = false

tool.Equipped:connect(function(mouse)
    print("Pistol Equipped")

    mouse.Button1Down:connect(function()
        print("Mouse Clicked")
    if (not isReload) and ammo > 0 then -- if Player is not reloading
            ammo = ammo - 1     

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


            local beam = Instance.new("Part", workspace)
            beam.BrickColor = BrickColor.new("Gray")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = 0.75
            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, 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(30)
                end
        end
    else
        tool.Name = "Reloading"
                isReload = true
                wait(2)
                tool.Name = "Pistol"
                ammo = 8
                print("Reloaded")
                isReload = false
    end

 --reloading
    end)
end)
Ad

Answer this question