Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I properly set the bullet hole direction with FastCast?

I've got the hole position working but was wondering how to face the direction every time the FastCast ray hits.

-- Server

local rs = game:GetService('ReplicatedStorage)
local e_Impact = rs.Remotes.Events.ImpactEvent

function OnRayHit(cast, raycastResult, segmentVelocity, cosmeticBulletObject)
	local hitPart = raycastResult.Instance
	local hitPoint = raycastResult.Position
	local normal = raycastResult.Normal
	
	if hitPart ~= nil and hitPart.Parent ~= nil then
		if hitPart.Name == 'Head' then
			local humanoid = hitPart.Parent:FindFirstChild("Humanoid") 
			if humanoid then
				humanoid:TakeDamage(Headshot)
			end
		elseif hitPart.Name ~= 'Head' then
			local humanoid = hitPart.Parent:FindFirstChild("Humanoid") 
			if humanoid then
				humanoid:TakeDamage(Bodyshot)
			end
		end
	end

	-- Right here. Tried to make it work the same way as ROBLOX's original RayCast, although is centers itself from said hitPart?
-- EDIT: dividing 'hitPoint' by 'normal' is so close but still not facing from where it came.
	local holeCFrame = CFrame.new(hitPoint, hitPoint / normal)
	e_Impact:FireAllClients(holeCFrame)
end

-- Client
local rs = game:GetService('ReplicatedStorage)
local e_Impact = rs.Remotes.Events.ImpactEvent

e_impact.OnClientEvent(function(hitpoint)
	local impact = client:WaitForChild('Impact')
	local hole = impact.BulletHole:Clone()
	hole.Parent = workspace
	hole.CFrame = hitpoint
	hole.Impact:Play()
end)

1 answer

I figured it out! I sent two arguments to the clients, 'hitPoint' and 'normal'. The server no longer calculates the CFrame. The 'hitPoint' is used for the position of where the FastCast ray hits and normal is used for the face of the direction from the ray.

-- server
function onRayhit(cast, raycastResult, segmentVelocity, cosmeticBulletObject)
	local hitPart = raycastResult.Instance
	local hitPoint = raycastResult.Position
	local normal = raycastResult.Normal
	--...
	e_Impact:FireAllClients(hitPoint, normal)
	--...
end


--Client
e_impact.OnClientEvent(function(hitpoint, normal)
	    local impact = client:WaitForChild('Impact')
	    local hole = impact.BulletHole:Clone()
	    hole.Parent = workspace
	    hole.CFrame = CFrame.new(hitpoint, hitpoint + normal)
	    hole.Impact:Play()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.