Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

script trouble

So i have another issue with a script on a custom sheet i have made. i cant get this script to generate the correct number the value needs to go up as the qty goes up. but i cant get it to do that what am i doing wrong? Is it because the qty can be changed with another button? Here is the script <script type="text/worker"><!-- Purchase Cost --> on('sheet:opened change:farm_qty', function (value) { getAttrs(['farm_qty'], function (value) { const farm_qty = +values.farm_qty || 0; const farm_price = 10 * farm_qty; let farm_purchase = farm_price; let farm_stone = 10; let farm_wood = 10; let farm_metal = 10; let farm_citizen = 1; let farm_coin = 1; let farm_food = 0; setAttrs({farm_purchase: farm_purchase}); setAttrs({farm_stone : farm_stone}); setAttrs({farm_wood : farm_wood}); setAttrs({farm_metal : farm_metal}); setAttrs({farm_citizen : farm_citizen}); setAttrs({farm_coin : farm_coin}); setAttrs({farm_food : farm_food}); }); }); </script> Here is the input lines. <input name="attr_farm_name" class="text110" type="text" value="Farm I"> <button name= "act_farm_d" class="button1" type="action">-</button> <input name="attr_farm_qty" class="text025" type="text" value="0"> <button name= "act_farm_p" class="button1" type="action">+</button> Here is the script for the + button <script type="text/worker"><!-- Purchase Button --> on('clicked:farm_p', function() { getAttrs(['farm_qty','coin_stock','farm_purchase','farm_wood','wood_stock','farm_stone','stone_stock','farm_metal','metal_stock'], function(values) { <!-- Qty Up --> const farm_qty = +values.farm_qty || 0; const new_farm_qty = farm_qty +1; <!-- Coin Cost --> const farm_purchase = +values.farm_purchase || 0; const coin_stock = +values.coin_stock || 0; const new_coin_stock = coin_stock - farm_purchase; <!-- Qty Metal --> const farm_metal = +values.farm_metal || 0; const metal_stock = +values.metal_stock || 0; const new_metal_stock = metal_stock - farm_metal; <!-- Qty Wood --> const farm_wood = +values.farm_wood || 0; const wood_stock = +values.wood_stock || 0; const new_wood_stock = wood_stock - farm_wood; <!-- Qty Stone --> const farm_stone = +values.farm_stone || 0; const stone_stock = +values.stone_stock || 0; const new_stone_stock = stone_stock - farm_stone; setAttrs({farm_qty: new_farm_qty}); setAttrs({coin_stock: new_coin_stock}); setAttrs({metal_stock: new_metal_stock}); setAttrs({wood_stock: new_wood_stock}); setAttrs({stone_stock: new_stone_stock}); }); }); </script> Here is a - button <script type="text/worker"><!-- Destroy Button --> on('clicked:farm_d', function() { getAttrs(['farm_qty'], function(values) { const farm_qty = +values.farm_qty || 0; const new_farm_qty = farm_qty -1; setAttrs({farm_qty: new_farm_qty}); }); }); </script>
1716419562

Edited 1716419599
GiGs
Pro
Sheet Author
API Scripter
First, some aesthetic and performance comments: You should not use multiple script blocks. All sheet workers should go in a single script block. Instead of something like this: <script type="text/worker"> /* first sheet worker */ </script> <script type="text/worker"> /* second sheet worker */ </script> <script type="text/worker"> /* third sheet worker */ </script> You should have <script type="text/worker"> /* first sheet worker */ /* second sheet worker */ /* third sheet worker */ </script> This isn't absolutely necessary, but makes your code more efficent and solves a major bug that has cropped up before. Likewise you should never do this in a sheet worker: setAttrs({farm_qty: new_farm_qty}); setAttrs({coin_stock: new_coin_stock}); setAttrs({metal_stock: new_metal_stock}); setAttrs({wood_stock: new_wood_stock}); setAttrs({stone_stock: new_stone_stock}); You should never have more than one setAttrs if you can avoid it. You can instead combine them like this: setAttrs({ farm_qty: new_farm_qty, coin_stock: new_coin_stock, metal_stock: new_metal_stock, wood_stock: new_wood_stock, stone_stock: new_stone_stock }); Getting The Sheet Worker Working I think I'm not understanding what you are trying to do. Those sheet workers look like they should work. Let's look at the - buutton because that is simplest. What are you expecting to happen when you click the button, and what is happening?
1716488423

Edited 1716488446
Nick
Pro
First thank you for all the help you have give me. sorry it is so bad. I will try to combine the scrips into one.  second what im trying to do is have the purchase cost increase based on the price of qty you have so if you have 20 it cost 20 coins for another once, if you have 5 it cost 50 coin for the next one and so on. When i click the button i want the cost to change based on the amount of farms you have. but when i click the button all that happens is the qty goes up. the price does not. 
1716492361
GiGs
Pro
Sheet Author
API Scripter
Does the - button work correctly?
yes and no when i click the button the qty increases but the purchase cost does not the coin should be 70, because i have 7 qty (see below)
1716507088
GiGs
Pro
Sheet Author
API Scripter
That's not the question I asked., i asked about the - button, which should be decreasing, not increasing.