Autorifile not working with raycast

i am trying to make a autorifile gun but i ran into two problems, the first problem is that the loop of the raycast ends after a few shots like this:

the other problem is when i spam click the tool and hold it, it gets way faster and seems to have no cooldown

local Debris = game:GetService("Debris")

local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local GunDamage = 30
equipped = false
shooting = false

LaserGun.Parent.Equipped:Connect(function(Mouse)
	equipped = true
	Mouse.Button1Down:Connect(function()
		shooting = true
		while shooting and equipped do
			Mouse.Button1Up:Connect(function()
				shooting = false
			end)
			local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
			local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
			LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
			local LaserBeam = Instance.new("Part", game.Workspace)
			LaserBeam.BrickColor = BrickColor.new("Bright green")
			LaserBeam.FormFactor = "Custom"
			LaserBeam.Material = "Neon"
			LaserBeam.Transparency = 0.25
			LaserBeam.Anchored = true
			LaserBeam.CanCollide = false

			local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
			LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
			LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
			wait(0.1)
			LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
			Debris:AddItem(LaserBeam, 0.1)

			if HitPart then
				local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")
				if not HitHumanoid then
					HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")

				end

				if HitHumanoid and HitHumanoid ~= Character.Humanoid then
					HitHumanoid:TakeDamage(GunDamage)
				end
			end
			wait(0.1)
		end
	end)
end)
LaserGun.Parent.Unequipped:Connect(function(Mouse)
	equipped = false
end)

edit
it seems to stop because i was pointing at something with humanoid, but i am not really sure

3 Likes

The community can’t help you without further info. Can you provide the scripts?

3 Likes

OH yeah i forgot the scripts :sweat_smile: im dumb k lemme add that rn sorry

2 Likes

Move your mouse.Button1Up event code into your .Equipped block.

2 Likes

oh huh, well it still stops after the raycast hit something, it works fine when i shoot it into the sky, does if statments stop a loop??

1 Like

To fix the faster shooting problem I would say use connections

local Debris = game:GetService("Debris")

local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local GunDamage = 30
equipped = false
shooting = false
local connection

LaserGun.Parent.Equipped:Connect(function(Mouse)
	equipped = true
	connection = Mouse.Button1Down:Connect(function()
		shooting = true
		while shooting and equipped do
			local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
			local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
			LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
			local LaserBeam = Instance.new("Part", game.Workspace)
			LaserBeam.BrickColor = BrickColor.new("Bright green")
			LaserBeam.FormFactor = "Custom"
			LaserBeam.Material = "Neon"
			LaserBeam.Transparency = 0.25
			LaserBeam.Anchored = true
			LaserBeam.CanCollide = false

			local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
			LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
			LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
			wait(0.1)
			LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
			Debris:AddItem(LaserBeam, 0.1)

			if HitPart then
				local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")
				if not HitHumanoid then
					HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")

				end

				if HitHumanoid and HitHumanoid ~= Character.Humanoid then
					HitHumanoid:TakeDamage(GunDamage)
				end
			end
			wait(0.1)
		end
	end)
end)
LaserGun.Parent.Unequipped:Connect(function(Mouse)
	equipped = false
end)

Mouse.Button1Up:Connect(function()
    shooting = false
connection:Disconnect() 
end)

By disconnecting the connection you avoid the event being listened multiple times