• Trying To Learn Lua, Need A Little Help
    13 replies, posted
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. [CODE]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)[/CODE] 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. [CODE]hook.Add("TTTOrderedEquipment", purchase, function() if LocalPlayer() :TTTOrderedEquipment(TTTOrderedEquipment) then[/CODE] 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
[QUOTE]Player:Give(item_ammo_pistol_ttt)[/QUOTE] Shouldn't this be a single dot, too?
[QUOTE=Coment;44832962]Shouldn't this be a single dot, too?[/QUOTE] I used the example on this page: [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index61ee.html[/url] 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: [url]http://facepunch.com/showthread.php?t=1391305&p=44813845&viewfull=1#post44813845[/url] 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: [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index6127.html[/url] [url]http://ttt.badking.net/guides/hooks[/url]
[QUOTE=Alig96;44833230][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: [url]http://facepunch.com/showthread.php?t=1391305&p=44813845&viewfull=1#post44813845[/url] 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: [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index6127.html[/url] [url]http://ttt.badking.net/guides/hooks[/url][/QUOTE] 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. [CODE]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)[/CODE]
[QUOTE=nikorev;44833882]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. [CODE]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)[/CODE][/QUOTE] Is the code being ran on both server and client? (Is it a shared file?)
[QUOTE=Alig96;44834149]Is the code being ran on both server and client? (Is it a shared file?)[/QUOTE] All I know is, I installed it in here. garrysmod\gamemodes\terrortown\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 [CODE] AddCSLuaFile() if CLIENT then ENT.Icon = "vgui/ttt/icon_radio" ENT.PrintName = "Extra Ammo" end [/CODE] 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\terrortown\entities\entities [CODE] 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 ) [/CODE]
[QUOTE=AnonTakesOver;44836280]No, he has is in the right spot. Add this at the top [CODE] AddCSLuaFile() if CLIENT then ENT.Icon = "vgui/ttt/icon_radio" ENT.PrintName = "purchaseAmmo_name" end [/CODE] 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\terrortown\entities\entities [CODE] 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 ) [/CODE][/QUOTE] 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. [CODE] 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") [/CODE] I changed this section to as follows, and now it works. [CODE] ply:GiveAmmo(40,"Pistol") ply:GiveAmmo(40,"SMG1") ply:GiveAmmo(20,"357") ply:GiveAmmo(12,"AlyxGun") ply:GiveAmmo(16,"Buckshot")[/CODE] 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
[QUOTE=nikorev;44836934] 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! [/QUOTE] 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.
[QUOTE=AnonTakesOver;44837011]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.[/QUOTE] 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
[QUOTE=nikorev;44841236]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? [/QUOTE] Read the wiki and experiment
Sorry, you need to Log In to post a reply to this thread.