Trying to create a simple gun command with ammo as the parameter

Hello guys, I’m trying to write a command that takes a number as parameter, and give the player a minigun with the number as the ammo, yet I keep getting a minigun with unlimited ammo despite the number.

This is my code:

RegisterCommand('weapon', function(source, args)

local ammo = args[1]

GiveWeaponToPed(GetPlayerPed(-1),"WEAPON_MINIGUN", ammo, false, true)

end,false)

What am I doing wrong?

Thanks in advanced.

RegisterCommand('weapon', function(source, args)
    local ammo = tonumber(args[1])
    SetPedAmmo(GetPlayerPed(-1), "WEAPON_MINIGUN", 0) -- Without this it will set the ammo to 250 when first trying it.
    GiveWeaponToPed(GetPlayerPed(-1),"WEAPON_MINIGUN", ammo, false, true)
end, false)
1 Like

The natives SetPedAmmo and GiveWeaponToPed require hashes. Typically strings are denoted as char* in the natives documentation. Deriving the work from @BabbaTundaee including the fixes:

RegisterCommand('weapon', function(source, args)
    local player = PlayerPedId()
    local hash = GetHashKey("WEAPON_MINIGUN")
    local ammo = tonumber(args[1])
    SetPedAmmo(player, hash, 0) -- Without this it will set the ammo to 250 when first trying it.
    GiveWeaponToPed(player, hash, ammo, false, true)
end, false)

Thank you for the comment, I don’t think I grasp the idea of hashes, whats a hash? and why can’t you use the string “weapon_minigun” as an argument but rather but it in a hash variable first?

oh and why do you first set ammo, but only then give the weapon, shouldn’t it be the opposite?

Thank you.

Where i put this code?