Trying To Learn Lua, Need A Little Help

Hello everyone. Nikorev back in need of more help. I am trying to learn lua, so excuse the various errors you might find. I am trying to make an item for TTT. This item is supposed to give detectives ammo. Like I said before, I am a very new lua coder, so I apologize for my possible errors that may seem very easy for you. The code is as follows.


EQUIP_AMMO = 12

SWEP.CanBuy = { ROLE_DETECTIVE }
SWEP.InLoadoutFor = nil
SWEP.LimitedStock = false
SWEP.EquipMenuData = {
   type = "Ammunition",
   desc = [[Gives you:
20 Sniper Ammo
60 SMG Ammo
20 Pistol Ammo
20 Deagle Ammo
20 Shotgun Ammo    ]]
};

hook.Add("TTTOrderedEquipment", purchase, function()
	if LocalPlayer() :TTTOrderedEquipment(TTTOrderedEquipment) then
		Player:Give(item_ammo_pistol_ttt)
		Player.Give(item_ammo_smg1_ttt)
		Player.Give(item_ammo_revolver_ttt)
		Player.Give(item_ammo_357_ttt)
		Player.Give(item_box_buckshot_ttt)
	end
end)

I bet I am completely wrong by the way, but why not give it a shot. I am trying to learn and mistakes will help me learn. Please give me tips on lua coding if you got any. The parts where I got really confused were these two lines.


hook.Add("TTTOrderedEquipment", purchase, function()
	if LocalPlayer() :TTTOrderedEquipment(TTTOrderedEquipment) then

I noticed that I used SWEP. content in the code, but I have no idea on how to make a passive item. I can make weapons, but not passive items that have an effect. I would like to also say thank you to those who have helped me in the past.

Cheers,
Nikorev

Shouldn’t this be a single dot, too?

I used the example on this page: http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index61ee.html

Still not 100% sure though.

[lua]
EQUIP_MONITOR = 8

local function CreateHeartbeat()
local heartbeatMonitor = {
id = EQUIP_MONITOR,
loadout = false,
type = “item_passive”,
material = “vgui/ttt/icon_monitor.png”,
name = “Heart Beat Monitor”,
desc = “Announces your death.”
}

  table.insert( EquipmentItems[ROLE_DETECTIVE], heartbeatMonitor )

end

hook.Add( “InitPostEntity”, “LoadEquipItemMonitor”, CreateHeartbeat )

hook.Add(“TTTOrderedEquipment”, “Monitor”, function(ply, equipment, is_item)
if equipment == EQUIP_MONITOR then
–Give player some ammo
ply:GiveAmmo(100,“smg1”)
end
end)
[/lua]

Slapped this together from here: http://forum.facepunch.com/showthread.php?t=1391305&p=44813845&viewfull=1#post44813845

What you have to do is, change the name, description and variable names and add what ever ammo types you want to give them, in the example this would give 100 Smg ammo

What this code basically does, is make the item in the top hook, then adds functionality for when its been bought in the second hook. I haven’t tested it but if you modify it properly should be fine

Some extra resources:

Call me an idiot, but it didn’t show up in the detective menu for me. Any idea why?

I forgot the code, here it is.


EQUIP_AMMO = 12

local function PurchseAmmo()
       local purchaseAmmo = {
              id = EQUIP_AMMO,
              loadout = false,
              type = "item_passive",
              material = "vgui/ttt/icon_monitor.png",
              name = "Extra Ammo",
              desc = [[Gives you:
20 Sniper Ammo
40 SMG Ammo
40 Pistol Ammo
12 Deagle Ammo
16 Shotgun Ammo    ]]
      }

      table.insert( EquipmentItems[ROLE_DETECTIVE], purchaseAmmo )
end 

hook.Add( "InitPostEntity", "LoadEquipItemAmmo", PurchaseAmmo )

hook.Add("TTTOrderedEquipment", "Ammo", function(ply, equipment, is_item)
	if equipment == EQUIP_AMMO then
		--Give player some ammo
		ply:GiveAmmo(40,"item_ammo_pistol_ttt")
		ply:GiveAmmo(40,"item_ammo_smg1_ttt")
		ply:GiveAmmo(20,"item_ammo_357_ttt")
		ply:GiveAmmo(12,"item_ammo_revolver_ttt")
		ply:GiveAmmo(16,"item_box_buckshot_ttt")
	end
end)

Is the code being ran on both server and client? (Is it a shared file?)

All I know is, I installed it in here. garrysmod\gamemodes errortown\entities\entities . I named it ttt_ammoload.

Put it in garrysmod/lua/autorun/ttt_ammoload.lua

No, he has is in the right spot.
Add this at the top



AddCSLuaFile()

if CLIENT then
   ENT.Icon = "vgui/ttt/icon_radio"
   ENT.PrintName = "Extra Ammo"
end


And after 20 years of me trying to work out why it wouldn’t work…
the function is named “PurchseAmmo”
you misspelled it, so nothing was being called >.<
You won’t believe how long and how much stuff I did trying to get it to work, only to realize that the function isn’t being called.

Working code, which is in garrysmod\gamemodes errortown\entities\entities



AddCSLuaFile()

if CLIENT then
   ENT.Icon = "vgui/ttt/icon_monitor.png"
   ENT.PrintName = "Extra Ammo"
end

if SERVER then
	hook.Add("TTTOrderedEquipment", "Ammo", function(ply, equipment, is_item)
		if equipment == EQUIP_AMMO then
			--Give player some ammo
			ply:GiveAmmo(40,"item_ammo_pistol_ttt")
			ply:GiveAmmo(40,"item_ammo_smg1_ttt")
			ply:GiveAmmo(20,"item_ammo_357_ttt")
			ply:GiveAmmo(12,"item_ammo_revolver_ttt")
			ply:GiveAmmo(16,"item_box_buckshot_ttt")
		end
	end)
end

EQUIP_AMMO = 9

local function PurchaseAmmo()
       local purchaseAmmo = {
              id = EQUIP_AMMO,
              loadout = false,
              type = "item_passive",
              material = "vgui/ttt/icon_monitor.png",
              name = "Extra Ammo",
              desc = [[Gives you:
20 Sniper Ammo
40 SMG Ammo
40 Pistol Ammo
12 Deagle Ammo
16 Shotgun Ammo    ]]
      }

      table.insert( EquipmentItems[ROLE_DETECTIVE], purchaseAmmo ) // Give detectives this equipment;
end 
hook.Add( "InitPostEntity", "LoadEquipItemAmmo", PurchaseAmmo )


Actually this code didn’t work for me. I realized that it was giving an entity to a player and trying to call it ammo, which couldn’t work.


			ply:GiveAmmo(40,"item_ammo_pistol_ttt")
			ply:GiveAmmo(40,"item_ammo_smg1_ttt")
			ply:GiveAmmo(20,"item_ammo_357_ttt")
			ply:GiveAmmo(12,"item_ammo_revolver_ttt")
			ply:GiveAmmo(16,"item_box_buckshot_ttt")


I changed this section to as follows, and now it works.


			ply:GiveAmmo(40,"Pistol")
			ply:GiveAmmo(40,"SMG1")
			ply:GiveAmmo(20,"357")
			ply:GiveAmmo(12,"AlyxGun")
			ply:GiveAmmo(16,"Buckshot")

All I need to know is, how to make this purchasable as many times as a player wants. I know SWEP.LimitedStock, but like before, I have no idea how to do this for passive items. Thank you for all of the help so far!

Cheers,
Nikorev

You would need to change “item_passive” to something else, I don’t know all the types, there should be a one use item type.

I will make it one use until I find a way. Thanks again for the help.

I got the icon working, I can officially call this complete. I am going to keep this a one time use so that it doesn’t get abused. Thanks for all of the help! Also, do you have any tips for a lua coder like me?

Cheers,
Nikorev

Read the wiki and experiment