• Why isn't this code working?
    7 replies, posted
Hello there. I am just starting with lua and I decided to remake Major Victory's Battle Cleanup addon for only personal use. It should remove weapons from map.. This is the code. However, it is doing nothing at all. And I don't know why... (I have put it in the right folder, tested with printChat commands) [lua] DroppedWeapons = {} GetDroppedWeapons() if #DroppedWeapons > 0 then DroppedWeapons = FadeThink(DroppedWeapons,"Weapon") end hook.Add("Think","NPCBC_Think",Think) function GetDroppedWeapons() local itemtable = {} itemtable = table.Add(itemtable, ents.FindByClass("weapon_")) itemtable = table.Add(itemtable, ents.FindByClass("ai_weapon_")) itemtable = table.Add(itemtable, ents.FindByClass("item_ammo_ar2_altfire")) itemtable = table.Add(itemtable, ents.FindByClass("item_healthvial")) for k,gun in pairs(itemtable) do if gun and gun:IsValid() and gun:GetOwner() == NULL then local Entry = { Entity = gun, EntryTime = CurTime() } table.insert(DroppedWeapons, Entry) end end end function FadeThink(itemtable,type) for k,v in pairs(itemtable) do if v and v.Entity:IsValid() then if type == "Weapon" and v.Entity:GetOwner() != NULL then v.Entity:Remove() itemtable[k] = nil end end end return itemtable end [/lua]
[QUOTE=Amand4;23222863]Hello there. I am just starting with lua and I decided to remake Major Victory's Battle Cleanup addon for only personal use. It should remove weapons from map.. This is the code. However, it is doing nothing at all. And I don't know why... (I have put it in the right folder, tested with printChat commands) [/QUOTE] Use [lua][/lua] tags [lua] DroppedWeapons = {} GetDroppedWeapons() if #DroppedWeapons > 0 then DroppedWeapons = FadeThink(DroppedWeapons,"Weapon") end // Hook, YourHookName, FunctionName hook.Add("Think","NPCBC_Think",Think) function GetDroppedWeapons() local itemtable = {} itemtable = table.Add(itemtable, ents.FindByClass("weapon_")) itemtable = table.Add(itemtable, ents.FindByClass("ai_weapon_")) itemtable = table.Add(itemtable, ents.FindByClass("item_ammo_ar2_altfire")) itemtable = table.Add(itemtable, ents.FindByClass("item_healthvial")) for k,gun in pairs(itemtable) do if gun and gun:IsValid() and gun:GetOwner() == NULL then local Entry = { Entity = gun, EntryTime = CurTime() } table.insert(DroppedWeapons, Entry) end end end function FadeThink(itemtable,type) for k,v in pairs(itemtable) do if v and v.Entity:IsValid() then if type == "Weapon" and v.Entity:GetOwner() != NULL then v.Entity:Remove() itemtable[k] = nil end end end return itemtable end [/lua] Where's Think declared and do you get any errors?
Hey, thanks for the reply. I updated my first post with [lua]. No, I don't get any errors. Declaring 'Think'? What does that involve?
[QUOTE=Amand4;23223289]Hey, thanks for the reply. I updated my first post with [lua]. No, I don't get any errors. Declaring 'Think'? What does that involve?[/QUOTE] Think is probably declared somewhere in the code above that you didn't paste since this addon is a whole thing, and copying just one part won't work.
OP read this [b][url=http://wiki.garrysmod.com/?title=Hook.Add]Hook.Add [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[code]function SaySomething(ply) ply:ChatPrint("You revived!") end hook.Add("PlayerSpawn", "RandomStuff", SaySomething)[/code] If this is in Sandbox, you would still recieve weapons and stuff, because you just hooked it, meaning your function will be called when the function you hooked it to is called.
Sorry, you need to Log In to post a reply to this thread.