Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

5 comments

0

Problem is debounce. Going to have to find another way to slow the fire rate of the gun...

0

Wow I just realized that somehow I commented the wrong line of code with

0

"unused right now" How did I do that? Oh well... Closing the question as I solved the problem.

0

I found your problem. Will answer later

0

Thanks lightpower but I solved this issue.

1 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)

1 comment

0

try this

This discussion is preserved as it was. New answers, comments, and votes are closed.