Creating bullet holes with RayCasting

I am fairly new to RayCasting and would like to know how I could create a bullet hole using RayCast or in simpler terms, find the exact position of where the ray hit.

Client:

		local ray = Ray.new(tool.Handle.CFrame.Position, (mouse.Hit.Position - tool.Handle.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		
		if part then
			local hitHumanoid = part.Parent:FindFirstChild("Humanoid")
			
			damageEvt:FireServer(hitHumanoid, part)
		end

Server:

damageEvt.OnServerEvent:Connect(function(player, hitHumanoid, part)

	if not hitHumanoid then
		hitHumanoid = part.Parent.Parent:FindFirstChild("Humanoid")
	end

	if hitHumanoid then
		hitHumanoid:TakeDamage(math.random(damageMin.Value, damageMax.Value))
	end
end)

Do not use Ray.New and workspace:FindPartOnRay. It is deprecated. Use workspace:Raycast() Instead.

Now, assuming youre using workspace:Raycast now, heres how to do it:

		local raycast = workspace:Raycast(tool.Handle.CFrame.Position, (mouse.Hit.Position - tool.Handle.CFrame.p).unit * 300) 
		if raycast then
			local function findHuman(instance:BasePart)
				local model = instance:FindFirstAncestorOfClass("Model")
				if model then
					if model:FindFirstChildOfClass("Humanoid") then
						local instanceHuman = model:FindFirstChildOfClass("Humanoid")
						return instanceHuman
					end 
				end
			end
			
			local human = findHuman()
			
			if human then
				damageEvt:FireServer(hitHumanoid, part)
			end
			
			local cframe = CFrame.lookAt(raycast.Position, raycast.Position + raycast.Normal)
			local bulletHole = nil -- switchout "nil" with your bullet hole in replicated storage or something
			local newBulletHole = bulletHole:Clone()
			newBulletHole.CFrame = cframe
		end

The rotation of the holes isn’t working.

What does it look like? Does it change rotation at all?

The rotation is kind of the reverse of what it should be.

image

Also, I didn’t use your exact script, but the bullet hole portion is the same:

damageEvt.OnServerEvent:Connect(function(player, mousePosition)
	
	local origin = muzzle.WorldCFrame.Position
	local direction = (mousePosition - origin).Unit*300
	local result = workspace:Raycast(origin, direction)

	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude

	if result then
		local part = result.Instance
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid:TakeDamage(math.random(damageMin.Value, damageMax.Value))
		end
	end
	
	local cframe = CFrame.lookAt(result.Position, result.Position + result.Normal)
	local bulletHole = game.ReplicatedStorage.BulletHole
	local newBulletHole = bulletHole:Clone()
	newBulletHole.Parent = workspace
	newBulletHole.CFrame = cframe
	
end)

Maybe you can fix this by simply changing the side of which the bullet decal is on?

But, maybe you can fix it by inversing the CFrames by using this as an example:

  • CFrame.Angles(math.rad(90),0 ,0)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.