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