Skip to content

Instantly share code, notes, and snippets.

@ihyajb
Last active February 13, 2024 05:12
Show Gist options
  • Save ihyajb/96513dc188947cc6f57e9a8f03803976 to your computer and use it in GitHub Desktop.
Save ihyajb/96513dc188947cc6f57e9a8f03803976 to your computer and use it in GitHub Desktop.
FiveM QBCore Weapon Throwing
--- CLIENT ---
local DroppedWeapons = {}
RegisterCommand('dropweapon', function()
local dict = 'anim@am_hold_up@male'; local name = 'shoplift_mid'
lib.requestAnimDict(dict)
TaskPlayAnim(PlayerPedId(),dict, name, 5.0, 1.5, -1, 48, 0.0, 0, 0, 0)
Wait(500)
local weaponEntity = GetCurrentPedWeaponEntityIndex(cache.ped)
local c = GetOffsetFromEntityInWorldCoords(weaponEntity, 0.0, 0.0, 0.0)
local r = GetEntityRotation(weaponEntity)
local RemovedItem = lib.callback.await('inventory:DropWeapon', false, cache.weapon, c, r)
if RemovedItem then
TriggerEvent('inventory:client:SetUnarmed') --? You dont have this event but, it just sets the player unarmed lol
end
end)
RegisterKeyMapping('dropweapon', 'Drops the currently selected weapon', 'keyboard', 'Y')
RegisterNetEvent('aj-scripts:client:DropWeapon', function(info, id)
if not IsPedArmed(cache.ped, 4) then return end
RequestWeaponAsset(info.model, 31, 0)
while not HasWeaponAssetLoaded(info.model) do
Wait(100)
end
local weaponAsset = CreateWeaponObject(info.model, 0, info.coords.x, info.coords.y, info.coords.z, true, 1.0)
RemoveWeaponAsset(info.model)
SetEntityRotation(weaponAsset, info.rot.x, info.rot.y, info.rot.z, 2, false)
SetEntityDynamic(weaponAsset, true)
info.handle = weaponAsset
DroppedWeapons[id] = info
SetEntityDrawOutline(weaponAsset, true)
SetEntityDrawOutlineShader(1)
SetEntityDrawOutlineColor(173, 92, 161, 255)
ApplyForceToEntity(weaponAsset, 0, 500.0, 0.0, 500.0, 0.0, 0.0, 0.0, 0, true, false, true, false, true)
end)
CreateThread(function()
while true do
local pc = GetEntityCoords(cache.ped)
for id, v in pairs(DroppedWeapons) do
if DoesEntityExist(v.handle) then
local EntityCoords = GetEntityCoords(v.handle)
local distance = #(EntityCoords - pc)
if distance <= 1.5 then
DrawText3D(vector3(EntityCoords.x, EntityCoords.y, EntityCoords.z), '~b~[E]~w~Pickup', distance)
if IsControlJustPressed(0, 38) then
lib.requestAnimDict('pickup_object')
TaskPlayAnim(PlayerPedId(),'pickup_object', 'pickup_low', 5.0, 1.5, -1, 48, 0.0, 0, 0, 0)
Wait(500)
StopAnimTask(PlayerPedId(), 'pickup_object', 'pickup_low', 1.0)
TriggerServerEvent('aj-scripts:server:AddWeapon', v, id)
DeleteEntity(v.handle)
DroppedWeapons[id] = nil
end
end
end
end
Wait(1)
end
end)
--- SERVER ---
local DroppedWeapons = {}
local function CreateWeaponID()
local id
repeat
id = math.random(1,9999)
until not DroppedWeapons[id]
return id
end
lib.callback.register('inventory:DropWeapon', function(source, weapon, coords, rot)
local src = source
local WeaponID = CreateWeaponID()
local Player = QBCore.Functions.GetPlayer(src)
local itemInfo = QBCore.Shared.Weapons[weapon]
local weaponItem = Player.Functions.GetItemByName(itemInfo["name"])
local RemovedItem = Player.Functions.RemoveItem(weaponItem.name, 1)
local data = {
name = weaponItem.name,
model = GetHashKey(weaponItem.name),
ammo = weaponItem.info.ammo or 0,
quality = weaponItem.info.quality or 100,
serie = weaponItem.info.serie or '????',
coords = coords,
rot = rot
}
DroppedWeapons[WeaponID] = true
TriggerClientEvent('aj-scripts:client:DropWeapon', -1, data, WeaponID)
return RemovedItem
end)
RegisterNetEvent('aj-scripts:server:AddWeapon', function(weaponInfo, id)
local src = source
if not DroppedWeapons[id] then return end
local Player = QBCore.Functions.GetPlayer(src)
local info = {
serie = weaponInfo.serie,
ammo = weaponInfo.ammo,
quality = weaponInfo.quality
}
Player.Functions.AddItem(weaponInfo.name, 1, false, info)
DroppedWeapons[id] = nil
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment