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

Why does it keep on shootin after i unequip it?

I scripted a gun, and it works perfectly fine. But the thing is, if I hold left mouse button down and unequip it at the same time, it still keep shooting. How do I fix this?


local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local shooting = false
local mouseconnection

tool.Equipped:Connect(function(mouse)

	mouseconnection=mouse.Button1Down:Connect(function()
		shooting = true
		while shooting do
			print("Mouse pressed!")
			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("Neon orange")
			beam.FormFactor = "Custom"
			beam.Material = "Neon"
			beam.Transparency = 0.25
			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.Barrel.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(45)
				end

				end
		
			mouse.Button1Up:Connect(function()

				shooting = false

			end)
			wait(0.3)
			end
			
	end)

end)

1 comment

0

Turn the shooting value off after you unequipped.

1 answer

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local shooting = false
local equipped = false -- REQUIRED SO PLAYER DOESNT SHOOT WHILE UNEQUIPPED
local mouseconnection

tool.Equipped:Connect(function(mouse)
	equipped = true
	
    mouseconnection=mouse.Button1Down:Connect(function()
        shooting = true
        while shooting and equipped do
            print("Mouse pressed!")
            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("Neon orange")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = 0.25
            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.Barrel.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(45)
                end

                end

            mouse.Button1Up:Connect(function()

                shooting = false

            end)
            wait(0.3)
            end

    end)

end)

Tool.Unequipped:Connect(function()
	If shooting == true then
		shooting = false
	end

	equipped = false -- VALUE
	shooting = false -- just in case.
end)

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