Jump to content

Whats wrong with this code?


Tycka

Recommended Posts

function weaponSwitch(weapon)  
   local wpn = getElementData(getLocalPlayer(), "currentweapon_1")  
   if source == getLocalPlayer() then  
     if wpn == "Sawn-Off Shotgun" then  
       if getElementData(getLocalPlayer(), "2Rnd. Slug") > 0 then  
         setElementData(getLocalPlayer(), "2Rnd. Slug", getElementData(getLocalPlayer(), "2Rnd. Slug") - 1)  
       end  
       elseif wpn == "AS50" then  
       if getElementData(getLocalPlayer(), "AS50 Mag") > 0 then  
         setElementData(getLocalPlayer(), "AS50 Mag", getElementData(getLocalPlayer(), "AS50 Mag") - 1)  
       end  
     else  
       local ammoName, _ = getWeaponAmmoType2(weapon)  
       if getElementData(getLocalPlayer(), ammoName) > 0 then  
         setElementData(getLocalPlayer(), ammoName, getElementData(getLocalPlayer(), ammoName) - 1)  
       end  
     end  
   end  
end  

Link to comment

There are a few deathtraps in your code, first off:

Always add this under the function() line, so you are avoiding unnecessary steps your server is doing.

if source == getLocalPlayer() then 

Second, if you fail to set the data as a number you'll get a nice little error that you're trying to compare strings to ints, use tonumber ( getElement...

if getElementData(getLocalPlayer(), "2Rnd. Slug") > 0 then 

Third, almost the same as second, it can create a lot of unwanted errors

setElementData(getLocalPlayer(), "2Rnd. Slug", getElementData(getLocalPlayer(), "2Rnd. Slug") - 1) 

Last off, are you calling this function with triggerClientEvent / triggerEvent? If not, check what your source is. :)

Link to comment
function weaponSwitch(weapon) 
   local wpn = getElementData(getLocalPlayer(), "currentweapon_1") 
   if source == getLocalPlayer() then 
     if wpn == "Sawn-Off Shotgun" then 
       if getElementData(getLocalPlayer(), "2Rnd. Slug") > 0 then 
         setElementData(getLocalPlayer(), "2Rnd. Slug", getElementData(getLocalPlayer(), "2Rnd. Slug") - 1) 
       end 
     else 
       local ammoName, _ = getWeaponAmmoType2(weapon) 
       if getElementData(getLocalPlayer(), ammoName) > 0 then 
         setElementData(getLocalPlayer(), ammoName, getElementData(getLocalPlayer(), ammoName) - 1) 
       end 
     end 
   end 
end 

When I use this its work great, when I add

  
       elseif wpn == "AS50" then 
       if getElementData(getLocalPlayer(), "AS50 Mag") > 0 then 
         setElementData(getLocalPlayer(), "AS50 Mag", getElementData(getLocalPlayer(), "AS50 Mag") - 1) 
       end 

Them code didint work

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...