Thread Tools Display Modes
03-03-22, 07:35 AM   #1
Firesong
A Deviate Faerie Dragon
Join Date: Jan 2022
Posts: 19
Question Change Button Text on Click Event

Hi all,

I have a button that I want to say "Start" when loaded and then say "Stop" when clicked.

Here is the lua creating the Fontstring and event:
BtnStopGo:SetScript("OnClick", StopStart)
local fs = BtnStopGo:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
fs:SetText("Start")
fs:SetPoint("CENTER")
BtnStopGo:SetFontString(fs)
BtnStopGo.text = fs

And here is the code I tried but it has errors:
function StopStart()
if boolStartStop == true
then
CommandStartStop.texture:SetColorTexture(255,0,0)
boolStartStop = false
fs:SetText("stop")
fs:SetText("Stop")
fs:SetPoint("CENTER")
BtnStopGo:SetFontString(fs)
BtnStopGo.text = fs
else
CommandStartStop.texture:SetColorTexture(0,255,0)
boolStartStop = true
fs:SetText("stop")
fs:SetText("Stop")
fs:SetPoint("CENTER")
BtnStopGo:SetFontString(fs)
BtnStopGo.text = fs
end
end

Here is the Bugsack error message:
"2x Grid\Grid.lua:641: attempt to index global 'fs' (a nil value)"

I've googled a lot and can't find the correct way to have text of a button change on clicking the button. Can anyone help please?
  Reply With Quote
03-03-22, 10:25 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
Code:
local button = CreateFrame("Button", nil, UIParent, "UIPanelButtonTemplate")
button:SetSize(100,20)
button:SetPoint("CENTER")
button:RegisterForClicks("AnyUp")
button:SetText("Start")
button:SetScript("OnClick", function(self,btn)
  if self:GetText()=="Start" then
    self:SetText("Stop")
  else
    self:SetText("Start")
  end
end)
button:SetMovable(true)
button:RegisterForDrag("LeftButton")
button:SetScript("OnDragStart", button.StartMoving)
button:SetScript("OnDragStop", button.StopMovingOrSizing)
button:SetScript("OnHide", button.StopMovingOrSizing)
button:Show()
This should make a minimal button for testing but your error mentions line 641, meaning there's a ton of code you're not showing.

The most probable cause is that fs does not exist inside StopStart because it is defined as a local in another scope.

Last edited by Dridzt : 03-05-22 at 04:21 AM.
  Reply With Quote
03-04-22, 12:09 AM   #3
Firesong
A Deviate Faerie Dragon
Join Date: Jan 2022
Posts: 19
Thumbs up

Thanks. That is so much more elegant than my effort as well as working!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Change Button Text on Click Event

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off