I am having trouble assigning a new label to a button that is below the function that the button calls.
local function buyNow(event) if ( "ended" == event.phase ) then --check if the player can afford a potion if (price\*quantity \<= playerStats.silver) then buyButton:setLabel ("Buy 1 potion")-- \<~~~ ERROR HERE end end end buyButton = widget.newButton { left = 50, top = 100, id = "button1", label = "Buy 1 potion", shape="roundedRect", width = 170, height = 40, fillColor = { default={ 0, 0, 0, 1 }, over={ 0, 0, 0, 1 } }, onEvent = buyNow }
Is there another way to access the button’s label? In other parts of the code I have it dynamically updating the label of the button, otherwise I wouldn’t be trying to change the label.
Just a heads up, it’s easier for people to help you fix any bugs if you post the error message that you are getting.
Normally I’d guess this is a scope issue (error message saying something like unable to index global “buyButton”), but your buyButton variable appears to be a global (it doesn’t have the word “local” in front of it). Is the code you’ve posted exactly as it appears in your code?
buyButton was temporarily changed to a global to see if it changed anything, The error message is "attempt to index global ‘buyButton’ (a nil value). Here is the full code, though I trimmed the fat because it does not pertain to the error message. I didn’t post the full code because I read that when posting on the forums, keep the code as short as possible.
local function buyNow(event) if ( "ended" == event.phase ) then --check if the player can afford a potion if (price\*quantity \<= playerStats.silver) then --buy some potions if (value == 1) then playerStats.potion1 = playerStats.potion1 + quantity elseif (value == 2) then playerStats.potion2 = playerStats.potion2 + quantity elseif (value == 3) then playerStats.potion3 = playerStats.potion3 + quantity elseif (value == 4) then playerStats.potion4 = playerStats.potion4 + quantity elseif (value == 5) then playerStats.potion5 = playerStats.potion5 + quantity elseif (value == 6) then playerStats.potion6 = playerStats.potion6 + quantity end playerStats.silver = playerStats.silver - price\*quantity quantity = 1 coinText.text = playerStats.silver saveData() buyButton:setLabel ("Buy 1 potion")--\<~~ Error here end end end local buyButton = widget.newButton { left = 50, top = 100, id = "button1", label = "Buy 1 potion", shape="roundedRect", width = 170, height = 40, fillColor = { default={ 0, 0, 0, 1 }, over={ 0, 0, 0, 1 } }, onEvent = buyNow }
Just a heads up, it’s easier for people to help you fix any bugs if you post the error message that you are getting.
Normally I’d guess this is a scope issue (error message saying something like unable to index global “buyButton”), but your buyButton variable appears to be a global (it doesn’t have the word “local” in front of it). Is the code you’ve posted exactly as it appears in your code?
buyButton was temporarily changed to a global to see if it changed anything, The error message is "attempt to index global ‘buyButton’ (a nil value). Here is the full code, though I trimmed the fat because it does not pertain to the error message. I didn’t post the full code because I read that when posting on the forums, keep the code as short as possible.
local function buyNow(event) if ( "ended" == event.phase ) then --check if the player can afford a potion if (price\*quantity \<= playerStats.silver) then --buy some potions if (value == 1) then playerStats.potion1 = playerStats.potion1 + quantity elseif (value == 2) then playerStats.potion2 = playerStats.potion2 + quantity elseif (value == 3) then playerStats.potion3 = playerStats.potion3 + quantity elseif (value == 4) then playerStats.potion4 = playerStats.potion4 + quantity elseif (value == 5) then playerStats.potion5 = playerStats.potion5 + quantity elseif (value == 6) then playerStats.potion6 = playerStats.potion6 + quantity end playerStats.silver = playerStats.silver - price\*quantity quantity = 1 coinText.text = playerStats.silver saveData() buyButton:setLabel ("Buy 1 potion")--\<~~ Error here end end end local buyButton = widget.newButton { left = 50, top = 100, id = "button1", label = "Buy 1 potion", shape="roundedRect", width = 170, height = 40, fillColor = { default={ 0, 0, 0, 1 }, over={ 0, 0, 0, 1 } }, onEvent = buyNow }