Skip to content

Instantly share code, notes, and snippets.

@SuelTheDev
Created November 14, 2022 13:10
Show Gist options
  • Save SuelTheDev/695853d25f2bca30170c94f9d9297fc2 to your computer and use it in GitHub Desktop.
Save SuelTheDev/695853d25f2bca30170c94f9d9297fc2 to your computer and use it in GitHub Desktop.
Weapon On Back FiveM
local WEAPONS <const> = {
[`weapon_bat`] = `w_me_bat`,
[`weapon_petrolcan`] = `prop_ld_jerrycan_01`,
[`weapon_hazardcan`] = `prop_ld_jerrycan_01`,
[`weapon_carbinerifle`] = `w_ar_carbinerifle`,
[`weapon_carbinerifle_mk2`] = `w_ar_carbineriflemk2`,
[`weapon_assaultrifle`] = `w_ar_assaultrifle`,
[`weapon_assaultrifle_mk2`] = `w_ar_specialcarbine`,
[`weapon_bullpuprifle`] = `w_ar_bullpuprifle`,
[`weapon_bullpuprifle_mk2`] = `w_ar_bullpupriflemk2`,
[`weapon_advancedrifle`] = `w_ar_advancedrifle`,
[`weapon_microsmg`] = `w_sb_microsmg`,
[`weapon_assaultsmg`] = `w_sb_assaultsmg`,
[`weapon_smg`] = `w_sb_smg`,
[`weapon_smg_mk2`] = `w_sb_smgmk2`,
[`weapon_gusenberg`] = `w_sb_gusenberg`,
[`weapon_sniperrifle`] = `w_sr_sniperrifle`,
[`weapon_assaultshotgun`] = `w_sg_assaultshotgun`,
[`weapon_bullpupshotgun`] = `w_sg_bullpupshotgun`,
[`weapon_pumpshotgun`] = `w_sg_pumpshotgun`,
[`weapon_pumpshotgun_mk2`] = `w_sg_pumpshotgunmk2`,
[`weapon_musket`] = `w_ar_musket`,
[`weapon_heavyshotgun`] = `w_sg_heavyshotgun`,
[`weapon_firework`] = `w_lr_firework`,
}
local BONE_SETTING <const> = {
back = 24816,
coord = vec3(0.075, -0.15, -0.02),
rot = vec3(0.0, 165.0, 0.0)
}
local attached_weapons = {}
local function IsMeleeWeapon(hash)
return hash == `weapon_golfclub` or hash == `weapon_bat` or hash == `weapon_petrolcan` or hash == `weapon_hazardcan`
end
local function AttachWeapon(weaponhash, weaponmodel, boneNumber, coord, rot, isMelee)
local ped = PlayerPedId()
local bone = GetPedBoneIndex(ped, boneNumber)
local timeout = GetGameTimer()
RequestModel(weaponmodel)
while not HasModelLoaded(weaponmodel) and GetGameTimer() - timeout < 10000 do
Wait(15)
end
if HasModelLoaded(weaponmodel) then
attached_weapons[weaponhash] = {
handle = CreateObject(weaponmodel, GetEntityCoords(PlayerPedId()), true, false, true)
}
if isMelee then
coord = vec3(0.11, -0.14, 0.0)
rot = vec3(-75.0, 185.0, 92.0)
end
if weaponmodel == `prop_ld_jerrycan_01` then
coord = vec3(coord.x + 0.3, coord.y, coord.z)
end
AttachEntityToEntity(attached_weapons[weaponhash].handle, PlayerPedId(), bone, coord, rot, 1, 1, 0, 0, 2, 1)
end
end
Citizen.CreateThread(function()
while true do
local ped = PlayerPedId()
for w, wm in pairs(WEAPONS) do
if HasPedGotWeapon(ped, w, false) then
if not attached_weapons[w] and GetSelectedPedWeapon(ped) ~= w then
AttachWeapon(w, wm, BONE_SETTING.back, BONE_SETTING.coord, BONE_SETTING.rot, IsMeleeWeapon(w))
else
if attached_weapons[w] and GetSelectedPedWeapon(ped) == w then
print("deleted")
DetachEntity(attached_weapons[w].handle, true, false)
DeleteObject(attached_weapons[w].handle)
attached_weapons[w] = nil
end
end
end
end
Wait(100)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment