Jump to content

lock


xyz

Recommended Posts

So I'm making a script but wondering how do I check if someone has a weapon, if he does then do something.

For example, if someone does /ak, then if the someone has AK-47, something happens.

Thanks in advance

Edited by Guest
Link to comment
function getPedWeapons(ped) 
    local playerWeapons = {} 
    if ped and isElement(ped) and getElementType(ped) == "ped" or getElementType(ped) == "player" then 
        for i=2,9 do 
            local wep = getPedWeapon(ped,i) 
            if wep and wep ~= 0 then 
                table.insert(playerWeapons,wep) 
            end 
        end 
    else 
        return false 
    end 
    return playerWeapons 
end 

https://wiki.multitheftauto.com/wiki/GetPedWeapons

Returns a table

From this you have 2 options

A: Check if the weapon is in that table

B: Simplify the code above and combine it into the code you are working on, and make it return true / false if it finds that weapon.

Link to comment
function removeak(player) 
        takeWeapon(player, 30) 
end 
addCommandHandler("removeak", removeak) 

Here's the script I want it to check if the player has AK, if he has then remove it. If not, then output to chatbox that he doesn't have an AK.

Could you make this into it? I don't really understand tables yet.

Link to comment

I did it on my mobile phone, so i didn't test it.

  
function removeak(player) 
    if hasPedWeapon(player, 30) then 
        takeWeapon(player, 30) 
    end 
end 
addCommandHandler("removeak", removeak) 
  
function hasPedWeapon(ped, weaponID) 
    if ped and isElement(ped) and getElementType(ped) == "ped" or getElementType(ped) == "player" and weaponID then 
        for i=2,9 do 
            local wep = getPedWeapon(ped,i) 
            if wep and wep == weaponID then 
                return true 
            end 
        end 
    end 
    return false 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...