To attach an object to another in FiveM, you can use the AttachEntityToEntity native function. Here's a script to attach a pallet object to a flatbed when the pallet is not connected to a forklift:
local flatbedModel = GetHashKey("flatbed")
local palletModel = GetHashKey("prop_pallet_03a") -- change to the specific pallet model you need
local forkliftModel = GetHashKey("forklift")
local attachedPallet = nil
Citizen.CreateThread(function()
RequestModel(flatbedModel)
while not HasModelLoaded(flatbedModel) do
Wait(1)
end
local flatbed = CreateVehicle(flatbedModel, 50.0, 50.0, 50.0, 90.0, true, true)
SetEntityAsMissionEntity(flatbed, true, true)
SetVehicleNumberPlateText(flatbed, "FLATBED")
TaskWarpPedIntoVehicle(PlayerPedId(), flatbed, -1)
RequestModel(palletModel)
while not HasModelLoaded(palletModel) do
Wait(1)
end
local pallet = CreateObject(palletModel, 55.0, 55.0, 55.0, true, true, true)
while true do
Wait(0)
local playerPed = PlayerPedId()
local vehicle = GetVehiclePedIsIn(playerPed, false)
if not IsEntityAttachedToEntity(pallet, vehicle) then
local pCoords = GetEntityCoords(pallet)
local vCoords = GetEntityCoords(vehicle)
if vehicle ~= 0 and IsThisModelAForklift(GetEntityModel(vehicle)) and #(pCoords - vCoords) < 5.0 then
AttachEntityToEntity(pallet, vehicle, GetEntityBoneIndexByName(vehicle, "misc_a"), 0, 0, 0, 0, 0, 0, false, false, false, false, 2, true)
attachedPallet = pallet
elseif vehicle ~= 0 and GetEntityModel(vehicle) == flatbedModel and #(pCoords - vCoords) < 5.0 and attachedPallet == nil then
AttachEntityToEntity(pallet, vehicle, GetEntityBoneIndexByName(vehicle, "lodestar_wing"), 0, 0, 0, 0, 0, 0, false, false, false, false, 2, true)
end
end
if IsEntityAttachedToEntity(attachedPallet, playerPed) and GetVehiclePedIsIn(playerPed, false) ~= 0 and IsThisModelAForklift(GetEntityModel(GetVehiclePedIsIn(playerPed, false))) then
DetachEntity(attachedPallet, true, true)
attachedPallet = nil
end
end
end)
Make sure you replace prop_pallet_03a and forklift with the correct hashes for the pallet and forklift objects you want to use. The script checks if the pallet is not connected to a forklift and if it is not connected, it attaches the pallet to the flatbed vehicle. If the pallet is connected to the forklift, you can detach it by driving the forklift close to the flatbed.