The position of the bullet hole is not correct

Hello im have problem with bullet hole.
robloxapp-20210417-2133489.wmv
The fact is that when I shoot, the hole appears at the mouse position, and not where Ray hit. Im don’t know how to fix this.
Server script:

tool.Fire.OnServerEvent:Connect(function(player, mouseHit, rate)
	if script.Parent.CanFire.Value == false then
		script.Parent.CanFire.Value = true
		Ammo.Value = Ammo.Value - 1
		local fires = tool.Handle.Fire:Clone()
		fires.Parent = tool.Handle
		fires:Play()
		local ray = Ray.new(tool.Handle.GunFirePoint.WorldCFrame.p, (mouseHit.p - tool.Handle.GunFirePoint.WorldCFrame.p).unit * 300)
		local hit, position, normal = game.Workspace:FindPartOnRay(ray, player.Character, false, true)
		local distance = (position - tool.Handle.GunFirePoint.WorldCFrame.p).magnitude
		local part = Instance.new("Part")
		part.Name = "Bullet"
		part.Anchored = true
		part.CanCollide = false
		part.Material = "Neon"
		part.BrickColor = BrickColor.new("Gold")
		part.Size = Vector3.new(0.1, 0.1, distance)
		part.CFrame = CFrame.new(position, tool.Handle.GunFirePoint.WorldCFrame.p) * CFrame.new(0, 0, - distance / 2)
		part.Parent = workspace
		if hit and hit.Parent ~= workspace.BulletHoles then
			if hit.Parent and hit.Parent ~= workspace.BulletHoles then
				
				local hole = game.ReplicatedStorage.Metal:Clone()
				hole.Name = "Bullet Hole"
				hole.Parent = workspace.BulletHoles
				hole.Position = mouseHit.p
				hole.CFrame = CFrame.new(hole.Position, hole.Position + normal)

				local weld = Instance.new("Weld")
				weld.Part0, weld.Part1 = hit,hole
				weld.C0 = hit.CFrame:Inverse()
				weld.C1 = hole.CFrame:Inverse()
				weld.Parent = hole
				
				game:GetService("Debris"):AddItem(hole, 8)
				if hit.Parent:FindFirstChild("Humanoid") and hit.Name == "Head"  then
					hit.Parent.Humanoid:TakeDamage(math.random(137,168))
				end
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid:TakeDamage(math.random(34,63))
				elseif hit.Parent:FindFirstChild("Humanoid") and hit.Name == "Left Leg" or hit.Name == "Right Leg" then
					hit.Parent.Humanoid:TakeDamage(math.random(23,45))
				end
			end
		end
		game:GetService("Debris"):AddItem(part, 0.03)
	end
	wait(rate)
	script.Parent.CanFire.Value = false
end)

(Please make a script and explain the lines to me. I don’t know much programming)

The position and rotation of the bullet hole is defined as:

Which is unfortunate cause you are not using your raycast position from:

I am not sure if the normal is used properly, but the position should be the hit of your raycast. Also, there is no need to set hole.Position if you are setting hole.CFrame.

I would probably define ‘hole’ as:

// Your old code
local hole = game.ReplicatedStorage.Metal:Clone()
hole.Name = "Bullet Hole"
hole.Parent = workspace.BulletHoles
// New CFrame
hole.CFrame = CFrame.new(position, position + normal)

Beware that Workspace:FindPartOnRay is deprecated and Workspace:Raycast is recommended.

4 Likes

Thanks for the explanation! I’ll try it now.

You did use the raycast normal properly (see CFrame from normal).

1 Like

Thanks! Everything works perfectly! Now I will know how to correctly define a CFrame.
Thanks to people like you, I will learn Luau better.

1 Like