How to get a gib from position?

So lets say I have a table with gib names with Vector3s assigned to them:

local GibOffsets = {
	["Head"] = {
		["Jaw"] = Vector3.new(0,-0.5,-0.5) --Front Bottom
	}
}

And I have a Raycast Result Vector3:

local RayPosition = RaycastResult.Position

How would I get the closest gib to the RayPosition? An example:

This is what I currently use:

--# Variables
local GibOffsets = {
	["Head"] = {
		["Jaw"] = Vector3.new(0, -0.5, -0.5)
	}
}
local Settings = {
	GibFromPositionRange = 2
}

--# Functions
function module:GetGibFromPosition(Object, RayPosition)
	if not GibOffsets[Object.Name] then
		return
	end
	local ObjectPosition = Object.Position
	local ObjectGib = nil
	for Index, V3Offset in pairs(GibOffsets[Object.Name]) do
		local PositionIncrement = CFrame.new(ObjectPosition + V3Offset/2).Position
		if (PositionIncrement - RayPosition).Magnitude <= Settings.GibFromPositionRange then
			ObjectGib = Index
		end
	end
	return ObjectGib
end
1 Like