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

My gun script works but when auto is true there is a small glitch. How can I fix it?[solved]

Asked by
AZDev 590 Moderation Voter
8 years ago

Yesterday I asked about this script because it was giving an error and not working much at all.

Today I rewrote the script completely and now my previous problem is fixed.

When auto is enabled the gun fires one shot then a waits half second then resumes firing fully automatic.

The code:

--player variables
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()

--gun variables
local gun = script.Parent -- unused right now
local ammo = 10 -- unused right now
local reloading = false
local auto = true
local firing = false

if not game.workspace:FindFirstChild("Bullet") then
    local bullet_storage = Instance.new("Folder", workspace)
    bullet_storage.Name = "Bullet"
else
    print('Bullet Storage located!')
end

function getBullet()
    local mh = mouse.Hit
    local B = Instance.new("Part", workspace.Bullet)
    B.CFrame = gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
    B.CFrame = CFrame.new(B.Position, mh.p)
    B.FormFactor = "Custom"
    B.Size = Vector3.new(0.2, 0.2, 0.2)
    B.BrickColor = BrickColor.Black()
    B.CanCollide = true
    local velo = Instance.new("BodyVelocity", B)
    velo.velocity = B.CFrame.lookVector * 120
    velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    B.Touched:connect(function(hit)
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(10, 25)
        end
    end)
end

function autofire() -- problem would be in here, I think
local debounce = false
    while firing == true do
        wait(0.1)
        if debounce == false then
            debounce = true
            local mh = mouse.Hit
            local B = Instance.new("Part", workspace.Bullet)
            B.CFrame = gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
            B.CFrame = CFrame.new(B.Position, mh.p)
            B.FormFactor = "Custom"
            B.Size = Vector3.new(0.2, 0.2, 0.2)
            B.BrickColor = BrickColor.Black()
            B.CanCollide = true
            local velo = Instance.new("BodyVelocity", B)
            velo.velocity = B.CFrame.lookVector * 120
            velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            B.Touched:connect(function(hit)
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:TakeDamage(10, 25)
                end
            wait(0.8)
            debounce = false
            end)
        end
    end
end

mouse.Button1Down:connect(function()
    if auto == true then
        firing = true
        autofire()
    else
        getBullet()
    end
end)

mouse.Button1Up:connect(function()
    if firing == true then
        firing = false
    end
end)

Thanks!

0
Problem is debounce. Going to have to find another way to slow the fire rate of the gun... AZDev 590 — 8y
0
Wow I just realized that somehow I commented the wrong line of code with AZDev 590 — 8y
0
"unused right now" How did I do that? Oh well... Closing the question as I solved the problem. AZDev 590 — 8y
0
I found your problem. Will answer later lightpower26 399 — 8y
0
Thanks lightpower but I solved this issue. AZDev 590 — 8y

1 answer

Log in to vote
-4
Answered by 8 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

here i hope this will help. i changed the (.01) to (0)

--player variables
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()

--gun variables
local gun = script.Parent -- unused right now
local ammo = 10 -- unused right now
local reloading = false
local auto = true
local firing = false

if not game.workspace:FindFirstChild("Bullet") then
    local bullet_storage = Instance.new("Folder", workspace)
    bullet_storage.Name = "Bullet"
else
    print('Bullet Storage located!')
end

function getBullet()
    local mh = mouse.Hit
    local B = Instance.new("Part", workspace.Bullet)
    B.CFrame = gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
    B.CFrame = CFrame.new(B.Position, mh.p)
    B.FormFactor = "Custom"
    B.Size = Vector3.new(0.2, 0.2, 0.2)
    B.BrickColor = BrickColor.Black()
    B.CanCollide = true
    local velo = Instance.new("BodyVelocity", B)
    velo.velocity = B.CFrame.lookVector * 120
    velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    B.Touched:connect(function(hit)
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(10, 25)
        end
    end)
end

function autofire() -- problem would be in here, I think
local debounce = false
    while firing == true do
        wait(0)
        if debounce == false then
            debounce = true
            local mh = mouse.Hit
            local B = Instance.new("Part", workspace.Bullet)
            B.CFrame = gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
            B.CFrame = CFrame.new(B.Position, mh.p)
            B.FormFactor = "Custom"
            B.Size = Vector3.new(0.2, 0.2, 0.2)
            B.BrickColor = BrickColor.Black()
            B.CanCollide = true
            local velo = Instance.new("BodyVelocity", B)
            velo.velocity = B.CFrame.lookVector * 120
            velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            B.Touched:connect(function(hit)
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:TakeDamage(10, 25)
                end
            wait(0.8)
            debounce = false
            end)
        end
    end
end

mouse.Button1Down:connect(function()
    if auto == true then
        firing = true
        autofire()
    else
        getBullet()
    end
end)

mouse.Button1Up:connect(function()
    if firing == true then
        firing = false
    end
end)
0
try this USArmyRanger2003 0 — 8y
Ad

Answer this question