# Can I use LUA to actively replace an item with another?

**URL:** <https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673>\
**Category:** The Toolbox\
**Created:** [February 3, 2019, 5:51pm UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673 "2019-02-03T17:51:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeWolf](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@DeWolf](https://discourse.cataclysmdda.org/u/DeWolf)\
**Post date:** [February 3, 2019, 5:51pm UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/1 "2019-02-03T17:51:38Z")

</div>

Is there a way to use a LUA to make a mod that automatically replaces all instances of an item in the world it is used in with a different item?

---

<div class="post-metadata">

**Author:** ![Poragon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/poragon/32/4161_2.png) [@Poragon](https://discourse.cataclysmdda.org/u/Poragon)\
**Post date:** [February 3, 2019, 10:39pm UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/2 "2019-02-03T22:39:55Z")

</div>

Should be possible, whip up a function that checks for the existance of the item in some way and returns the item (searching X tiles around the player for the item, or something along those lines. PoraComp mop actions does something similar and would be a good way to see how it’s done. ) Then just rewrite the item.type attribute to whatever item you want it replaced with.

---

<div class="post-metadata">

**Author:** ![DeWolf](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@DeWolf](https://discourse.cataclysmdda.org/u/DeWolf)\
**Post date:** [February 3, 2019, 10:55pm UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/3 "2019-02-03T22:55:19Z")

</div>

Thank you. And now time to experiment and Frankencode.

---

<div class="post-metadata">

**Author:** ![Zhilkin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zhilkin/32/3749_2.png) [@Zhilkin](https://discourse.cataclysmdda.org/u/Zhilkin)\
**Post date:** [February 4, 2019, 6:19am UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/4 "2019-02-04T06:19:44Z")

</div>

What do you want a kind of a blacklist or what?

---

<div class="post-metadata">

**Author:** ![darktoes](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@darktoes](https://discourse.cataclysmdda.org/u/darktoes)\
**Post date:** [February 4, 2019, 7:15am UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/5 "2019-02-04T07:15:41Z")

</div>

Actually, there’s JSON code that does this. I think it’s an obsoletion entry or something?

---

<div class="post-metadata">

**Author:** ![DeWolf](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@DeWolf](https://discourse.cataclysmdda.org/u/DeWolf)\
**Post date:** [February 4, 2019, 7:06pm UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/6 "2019-02-04T19:06:21Z")

</div>

What I am trying to do is make the combat knife add a new underbarrel mod slot when it is attached so that it doesn’t block a foregrip or something like that. However that did not work so my first assumption was that it was a hard coded thing (like how it could originally **only** be attached to the underbarrel). So instead of make a mod that modifies every instance of a vanilla knife spawn why not just make a mod that when I was in proximity it would replace the old one with a new one thus circumventing that problem. Unfortunately I just tried making a modded copy of the combat knife and it also does not add that additional underbarrel slot so yeah that is kinda disappointing.

---

<div class="post-metadata">

**Author:** ![DeWolf](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@DeWolf](https://discourse.cataclysmdda.org/u/DeWolf)\
**Post date:** [February 10, 2019, 2:57am UTC](https://discourse.cataclysmdda.org/t/can-i-use-lua-to-actively-replace-an-item-with-another/18673/7 "2019-02-10T02:57:06Z")

</div>

NEVERMIND I found out why the add underbarrel mod on the combat knife wasn’t working. I just had it set up wrong (needs to be in the gunmod data section of its entry).
