Help with raycast

So following up with my previous post with my gun, when ever I reload it the raycast seems to create 2 bullets or something like that because my ammo gets eaten up twice as fast as before and by some point can be eaten up by 1 second.

local tool = script.Parent

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local mouse = Player:GetMouse()
local UIS = game:GetService('UserInputService')

local Aim = Humanoid:LoadAnimation(script.Parent.Hold)
local holster = Humanoid:LoadAnimation(script.Parent.Holster)


local Bullets = script.Parent.Config.Bullets
local Stock = script.Parent.Config.Stock
local Mag = script.Parent.Config.Mag
local Aimed = false
local Firing = false
local CanFire = true

--[[function SpinBarrels()
	local Barrels = script.Parent.Barrels
	
	
end--]]

function reload()
	
	local NewBullets = Mag.Value - Bullets.Value	
				
	for i = 1,NewBullets do 
		Bullets.Value = Bullets.Value + 1
		wait()
	end
	Stock.Value = Stock.Value - Mag.Value
	Firing = false
	Aimed = true
	CanFire = true
end


tool.Equipped:Connect(function(Mouse)
	holster:Play()
	game.ReplicatedStorage.GunGui:Clone().Parent = game.Players.LocalPlayer.PlayerGui
	
	local GUI = Player.PlayerGui:WaitForChild("GunGui")
	
	GUI.Frame.GunName.Text = script.Parent.Name
	
	GUI.Frame.Ammo.Text = Bullets.Value.."/"..Stock.Value
	
	Bullets.Changed:Connect(function()
		GUI.Frame.Ammo.Text = Bullets.Value.."/"..Stock.Value
	end)
	
	UIS.InputBegan:connect(function(input)
		if input.KeyCode == Enum.KeyCode.R then
			reload()
			print("reloading")
		end
	
	Mouse.Button2Down:Connect(function()
		holster:Stop()
		Aim:Play()
		
		Mouse.Icon = "rbxassetid://919035852"
		Aimed = true
		
		Player.CameraMode = Enum.CameraMode.LockFirstPerson
	end)
	Mouse.Button2Up:Connect(function()
		Aim:Stop()
		holster:Play()
		
		Player.CameraMode = Enum.CameraMode.Classic
		Player.CameraMinZoomDistance = 10
		wait()
		Player.CameraMinZoomDistance = 0
		Mouse.Icon = ""
		Aimed = false
		Firing = false
	end)
	Mouse.Button1Down:Connect(function()
		if Aimed == true then
		wait(1.044)
		if Aimed == true then
		Firing = true
		if CanFire == true then
		while Firing == true do
			script.Parent.Sounds.Minigun_2:Play()
			Bullets.Value = Bullets.Value - 1
			game.ReplicatedStorage.ShootEvent:FireServer(script.Parent.Hole.CFrame.Position, mouse.Hit.Position)
			wait(0.05)
			if Bullets.Value == 0 then
				reload()
				Firing = false
				Aimed = false
				CanFire = false
			end
		end
		end
		end
	end
	end)
	Mouse.Button1Up:Connect(function()
		Firing = false
	end)
	end)
end)

tool.Unequipped:Connect(function()
	holster:Stop()
	Aim:Stop()
	
	Player.PlayerGui:WaitForChild("GunGui"):Destroy()
end)

Local gun script ^

game.ReplicatedStorage.ShootEvent.OnServerEvent:Connect(function(Player, FromP, ToP, ID)
		local ray = Ray.new(FromP, (ToP - FromP).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true)
 
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
 
		local distance = (FromP - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(FromP, 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(100)
			end
		end
end)

Server script for raycast ^

Update: I noticed that it shoots 2 bullets at a time than 1 so it may be something in the while loop

It may be because of where you begin listening for the Button1Down event. What your code is doing now is every time the tool is equipped, the script starts listening for the Button1DownEvent each time, and so after you equip the tool 5 times, there are 5 seperate functions that are being called from 1 mouse click.

You can fix it by removing the Mouse.Button1Down:Connect(function() from the tool.Equipped:Connect(function(Mouse) function, I also suggest you remove the Button2Up,Button2Down,InputBegan,Bullets.Changed:connect(function(), as they will also begin to “pile up” with every time you equip the tool. They can all be placed outside of the Equipped function.

1 Like