My gun is not shooting right

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want my automatic gun to be more cleaner and fix this bug with it.

  1. What is the issue? Include screenshots / videos if possible!

So I am trying to make my gun automatic(and I did). But the problem is, is that when I hold down the mouse button. it doesn’t shoot instantly, instead, it just waits a few seconds then fires.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have looked over a lot of dev forum posts, but none of them seem to be my answer.

There are 2 scripts, the client and the server

Client

local Holdanimation = script.Parent:WaitForChild("HoldAnimation")

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local Tool = script.Parent

local CanShoot = false

local Configurations = Tool:WaitForChild("Configurations")

local Firerate = Configurations:WaitForChild("Firerate").Value

local Ammo = Configurations:WaitForChild("Ammo").Value

local IsAutomatic = Configurations:WaitForChild("Automatic").Value

local Damage = Configurations:WaitForChild("Damage").Value

local ShootEvent = Tool:WaitForChild("Shoot")

local Reloadtime = Configurations:WaitForChild("ReloadTime").Value

local MaxAmmo = Configurations:WaitForChild("MaxAmmo").Value

local UserInputService = game:GetService("UserInputService")

local reloading = false

script.Parent.Equipped:Connect(function()
	local Humanoid = script.Parent.Parent:WaitForChild("Humanoid")
	HoldAnimationTrack = Humanoid:LoadAnimation(Holdanimation)
	HoldAnimationTrack:Play()
	mouse.Button1Down:Connect(function()

		shooting = false
		if IsAutomatic then
			if not shooting then
				CanShoot = true
				if not reloading and CanShoot == true then
					local  pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
					while pressed do
						pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
						local MousePos = mouse.Hit.Position
						local Origin = Tool.Tip.Position
						CanShoot = true
						shooting = true
						repeat
							if CanShoot == true then
								Ammo = Ammo - 1
								ShootEvent:FireServer(MousePos,Origin)
								CanShoot = false
								wait(Firerate)
								CanShoot = true
							end

						until not shooting or Ammo < 1
					end

				end
			end
		end

	end)
	mouse.Button1Up:Connect(function()
		if not reloading then
			shooting = false
			if Ammo < 1 then
				reloading = true
				wait(Reloadtime)
				Ammo = MaxAmmo
				reloading = false
			end
		end
		shooting = false
	end)
end)

script.Parent.Unequipped:Connect(function()
	HoldAnimationTrack:Stop()
end)

Server


local Tool = script.Parent

local Configurations = Tool:WaitForChild("Configurations")

local Firerate = Configurations:WaitForChild("Firerate")

local Ammo = Configurations:WaitForChild("Ammo")

local IsAutomatic = Configurations:WaitForChild("Automatic")

local Damage = Configurations:WaitForChild("Damage")

local ShootEvent = Tool:WaitForChild("Shoot")

local Debris = game:GetService("Debris")

local range = Configurations:WaitForChild("Range")

local function CreateBullet(origin, direction)
	local midpoint = origin + direction/2
	
	local part = Instance.new("Part")
	part.Parent = game.Workspace
	part.Anchored = true
	part.CanCollide = false
	
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(.5,.5,direction.magnitude)
	
	Debris:AddItem(part,1)
end


ShootEvent.OnServerEvent:Connect(function(player,MousePosition,TipPos)
	local direction = (MousePosition - TipPos).Unit * range.Value
	
	local result = workspace:Raycast(TipPos, direction)
	
	if result then
		local Humanoid = result.Instance.Parent:FindFirstChild("Humanoid") or result.Instance.Parent.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			local ModelName = Humanoid.Parent.Name
			if game.Players:FindFirstChild(ModelName) then
				print("its not a bad guy")
			else
				print("its a bad guy")
			end
		end
	end
	
	CreateBullet(TipPos, direction)
	
end)

I also have a remote event and a folder that has configuration settings that I made, but I don’t think it has to do with this.