Raycast on gun weird

well, I was making another weapon for my project, as my weapon’s script worked, I decided to use it as a base to make future weapons, so I made an AK47, everything was fine, but for some reason, when I shoot the Raycast comes out and starts to descending with each shot, literally crossing the floor, I already checked everything but nothing helped.

viewmodel + AK:
image

the script:
`
local settings = {
[“cooldown”] = 0; – Time between shots
}

local currentAmmo = 30
local maxAmmo = 31
local lastShotTime = 0

function fireBullet()
if currentAmmo > 0 then
local currentTime = tick()
if currentTime - lastShotTime >= 0 then
shootAnimationTrack:Play()
lastShotTime = currentTime
currentAmmo = currentAmmo - 1

		local mouse = player:GetMouse()
		local direction = (mouse.Hit.p - player.Character.HumanoidRootPart.Position).Unit

		-- Configuração do Raycast
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {player.Character}

		-- Criação do Raycast
		local caboDaArma = viewmodel.AK47.Parts.boca
		local comprimentoDaArma = 3
		local ajustePosicao = Vector3.new(0, 0, 0) -- Ajuste a posição aqui. Neste exemplo, o raycast começa 1 stud abaixo e 1 stud atrás da boca da arma.
		local bocaPos = caboDaArma.Position + caboDaArma.CFrame.lookVector * comprimentoDaArma + ajustePosicao

		local raycastResult = workspace:Raycast(bocaPos, direction * 1000, raycastParams)

		if raycastResult then
			local character = raycastResult.Instance.Parent
			while character and not character:IsA("Model") do
				character = character.Parent
			end

			if character then
				local humanoid = character:FindFirstChildWhichIsA("Humanoid")
				if humanoid then
					local hitPartName = raycastResult.Instance.Name

					if hitPartName == "Head" then
						humanoid:TakeDamage(125)
					elseif hitPartName == "Torso" or hitPartName == "LowerTorso" then
						humanoid:TakeDamage(36)
					elseif hitPartName == "Left Arm" or hitPartName == "Right Arm" or hitPartName == "Left Leg" or hitPartName == "Right Leg" or hitPartName == "Leg" then
						humanoid:TakeDamage(17)
					end
				end
			end

			-- Desenhar a linha do Raycast para visualização
			local line = Instance.new("Part",workspace)
			line.Anchored = true
			line.CanCollide = false
			line.Name = "Bullet"
			line.BrickColor = BrickColor.new("New Yeller")
			line.FormFactor = "Custom"
			line.TopSurface = "Smooth"
			line.BottomSurface = "Smooth"
			local s = (raycastResult.Position - bocaPos).magnitude
			line.Size = Vector3.new(.2,.2,s)
			line.CFrame = CFrame.new(raycastResult.Position,bocaPos)*CFrame.new(0,0,-s/2)

			game:GetService("Debris"):AddItem(line, 0.5) -- Remove a linha após 0.5 segundos
		end

		wait(settings["cooldown"])
	end
end

end`
type or paste code here

I dont have time to help but ill fix the formatting so everyone else has an easier time reading the code

local settings = {
[“cooldown”] = 0; – Time between shots
}

local currentAmmo = 30
local maxAmmo = 31
local lastShotTime = 0

function fireBullet()
		if currentAmmo > 0 then
		local currentTime = tick()
		if currentTime - lastShotTime >= 0 then
		shootAnimationTrack:Play()
		lastShotTime = currentTime
		currentAmmo = currentAmmo - 1
		local mouse = player:GetMouse()
		local direction = (mouse.Hit.p - player.Character.HumanoidRootPart.Position).Unit

		-- Configuração do Raycast
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {player.Character}

		-- Criação do Raycast
		local caboDaArma = viewmodel.AK47.Parts.boca
		local comprimentoDaArma = 3
		local ajustePosicao = Vector3.new(0, 0, 0) -- Ajuste a posição aqui. Neste exemplo, o raycast começa 1 stud abaixo e 1 stud atrás da boca da arma.
		local bocaPos = caboDaArma.Position + caboDaArma.CFrame.lookVector * comprimentoDaArma + ajustePosicao

		local raycastResult = workspace:Raycast(bocaPos, direction * 1000, raycastParams)

		if raycastResult then
			local character = raycastResult.Instance.Parent
			while character and not character:IsA("Model") do
				character = character.Parent
			end

			if character then
				local humanoid = character:FindFirstChildWhichIsA("Humanoid")
				if humanoid then
					local hitPartName = raycastResult.Instance.Name

					if hitPartName == "Head" then
						humanoid:TakeDamage(125)
					elseif hitPartName == "Torso" or hitPartName == "LowerTorso" then
						humanoid:TakeDamage(36)
					elseif hitPartName == "Left Arm" or hitPartName == "Right Arm" or hitPartName == "Left Leg" or hitPartName == "Right Leg" or hitPartName == "Leg" then
						humanoid:TakeDamage(17)
					end
				end
			end

			-- Desenhar a linha do Raycast para visualização
			local line = Instance.new("Part",workspace)
			line.Anchored = true
			line.CanCollide = false
			line.Name = "Bullet"
			line.BrickColor = BrickColor.new("New Yeller")
			line.FormFactor = "Custom"
			line.TopSurface = "Smooth"
			line.BottomSurface = "Smooth"
			local s = (raycastResult.Position - bocaPos).magnitude
			line.Size = Vector3.new(.2,.2,s)
			line.CFrame = CFrame.new(raycastResult.Position,bocaPos)*CFrame.new(0,0,-s/2)

			game:GetService("Debris"):AddItem(line, 0.5) -- Remove a linha após 0.5 segundos
		end

		wait(settings["cooldown"])
	end
end
end
1 Like