From 186c99fee7d81a07598a1f6c19a42525a0a7046d Mon Sep 17 00:00:00 2001 From: darknight2590 <87971656+darknight2590@users.noreply.github.com> Date: Sun, 19 Dec 2021 08:15:26 +0200 Subject: [PATCH] add GetClosestBone --- client/functions.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client/functions.lua b/client/functions.lua index 4d2b947..b85accb 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -263,6 +263,29 @@ function QBCore.Functions.GetClosestObject(coords) return closestObject, closestDistance end +function QBCore.Functions.GetClosestBone(entity, list) + local playerCoords, bone, coords, distance = GetEntityCoords(PlayerPedId()) + + for _, element in pairs(list) do + local boneCoords = GetWorldPositionOfEntityBone(entity, element.id or element) + local boneDistance = #(playerCoords - boneCoords) + + if not coords then + bone, coords, distance = element, boneCoords, boneDistance + elseif distance > boneDistance then + bone, coords, distance = element, boneCoords, boneDistance + end + end + + if not bone then + bone = {id = GetEntityBoneIndexByName(entity, "bodyshell"), type = "remains", name = "bodyshell"} + coords = GetWorldPositionOfEntityBone(entity, bone.id) + distance = #(coords - playerCoords) + end + + return bone, coords, distance +end + -- Vehicle function QBCore.Functions.SpawnVehicle(model, cb, coords, isnetworked)