Weapon: Tried to use a nil entity!

Hello, I’m trying to use LocalPlayer():Weapon(), but It’s not working, here’s my code:



tipo = LocalPlayer():GetWeapon(table_ID[GetIndex(selecao)]) // table_ID is a table that got strings (weapon_smg1, weapon_ar2) and GetIndex is a function that returns where the weapon_ is on the table (returns a int)
net.Start("giveWeapon")
  net.WriteString(tipo)
net.SendToServer()
net.Start("giveAmmo")
  net.WriteInt(tipo:GetPrimaryAmmoType( ) , 16) // Error is here, is saying that GetPrimaryAmmoType is trying to use a nil value
  net.WriteInt(tipo:GetAmmoCount(tipo:GetPrimaryAmmoType( ) ) * 4, 16)
net.SendToServer()


Use an IsValid() check on tipo.

See part 2: https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html

Even if it isn’t for a HUD, it still applies as GetWeapon / GetActiveWeapon can be NULL

You need to check if the weapon is valid?

my code is now:



tipo = LocalPlayer():GetWeapon(table_ID[GetIndex(selecao)])
							if isValid(tipo) then
								net.Start("giveWeapon")
									net.WriteString(tipo)
								net.SendToServer()
								net.Start("giveAmmo")
									net.WriteInt(tipo:GetPrimaryAmmoType( ) , 16)
									net.WriteInt(tipo:GetAmmoCount(tipo:GetPrimaryAmmoType( ) ) * 4, 16)
								net.SendToServer()
								chat.AddText( Color( 0, 255, 0 ), "[Clã | BR]", nickteamcolour3, name3, Color( 255, 255, 255 ), " Você comprou uma " .. selecao )
								setCredits(getCredits() - table_price[GetIndex(selecao)])
							else
								chat.AddText( Color( 0, 255, 0 ), "[Clã | BR]", nickteamcolour3, name3, Color( 255, 255, 255 ), " Arma inválida!")
							end


Now i’m receiving this message:
attempt to call global ‘isValid’ (a nil value)

Capitals matter. IsValid, not isValid.

So, I tried this, but now I’m having a different problem, I’m trying to use GetWeapon after the Client recieve the weapon, but is giving me NULL (-1)



if IsValid(LocalPlayer():GetWeapon(table_ID[GetIndex(selecao)])) then // I'm giving the same string of the given weapon "table_ID[GetIndex(selecao)] = (weapon_smg1, weapon_ar2. weapon_357)"
    tipo = LocalPlayer():GetWeapon(table_ID[GetIndex(selecao)])
end