Attaching silencer to weapons with command/key press

I’m looking for standalone script that lets you attach silencer to your current weapon via command/key press.

I’m not sure if one exists. I’ll try to make one though and let you know when I finish it.

Here is what I would have done (partially tested) :

-- Store the hash key of every silencer component available
local silencerHashes = {
    `COMPONENT_AT_PI_SUPP_02`
    `COMPONENT_AT_PI_SUPP`
    `COMPONENT_AT_AR_SUPP_02`
    `COMPONENT_AT_SR_SUPP`
    `COMPONENT_AT_AR_SUPP`
}

-- Attach a silencer to the weapon the ped is holding
local function attachSilencerToCurrentWeapon()
    
	local added = false
	local playerPed = PlayerPedId()
	local currentWeapon = GetSelectedPedWeapon(playerPed)

	if currentWeapon ~= nil then

        local i = 1
        local l = #silencerHashes
        
        while i <= l and not added do

            local componentHash = silencerHashes[i]

            if DoesWeaponTakeWeaponComponent(currentWeapon, componentHash) then
                added = true
                GiveWeaponComponentToPed(playerPed, currentWeapon, componentHash)
            end

            i = i + 1

		end

	end

	return added
end

-- Register the function as a command
RegisterCommand('silencer', function()
    local handle = attachSilencerToCurrentWeapon()

    if handle then
        print("Silencer successfully attached !")
    else
        print("Current weapon isn't compatible with a silencer.")
    end
end)

Hope it helps you.

I found a way !!!

Just put this command on your vrp_player/client.lua :

RegisterCommand(“silencer”,function(source,args)
local ped = PlayerPedId()
if GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_COMBATPISTOL”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_COMBATPISTOL”),GetHashKey(“COMPONENT_AT_PI_SUPP”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_SMG”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_SMG”),GetHashKey(“COMPONENT_AT_PI_SUPP”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_PISTOL_MK2”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_PISTOL_MK2”),GetHashKey(“COMPONENT_AT_PI_SUPP_02”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_PUMPSHOTGUN_MK2”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_PUMPSHOTGUN_MK2”),GetHashKey(“COMPONENT_AT_SR_SUPP_03”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_CARBINERIFLE”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_CARBINERIFLE”),GetHashKey(“COMPONENT_AT_AR_SUPP”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_MICROSMG”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_MICROSMG”),GetHashKey(“COMPONENT_AT_AR_SUPP_02”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_ASSAULTRIFLE”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_ASSAULTRIFLE”),GetHashKey(“COMPONENT_AT_AR_SUPP_02”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_ASSAULTSMG”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_ASSAULTSMG”),GetHashKey(“COMPONENT_AT_AR_SUPP_02”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_PISTOL”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_PISTOL”),GetHashKey(“COMPONENT_AT_PI_SUPP_02”))
elseif GetSelectedPedWeapon(ped) == GetHashKey(“WEAPON_CARBINERIFLE_MK2”) then
GiveWeaponComponentToPed(ped,GetHashKey(“WEAPON_CARBINERIFLE_MK2”),GetHashKey(“COMPONENT_AT_AR_SUPP”))
end
end)

And Then, you can add every weapon you want in by the weapon name, and the attach seaching for its hash

hello, could you recreate that code for esx?