Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to change a TextButton's Name/Text then reference it later?

Asked by 4 years ago
Edited 4 years ago

So I have a script where I change the name and text of a TextButton when it is clicked (That part of the script works). When I try to change it after it has already been changed, then the script runs without error, but nothing changes.

LGui = script.Parent.LGui
    phsl = LGui.PHSL
    pncl = LGui.PNCL
    pnbl = LGui.PNBL
    back = LGui.back

CGui = script.Parent.CGui
    UniGui = CGui.UniGui
    TriGui = CGui.TriGui

LGui.PHSL.MouseButton1Click:Connect(function() -- Player clicks PHSL changes the names of the leagues to games,shop,and help on the GUI and Explorer
    phsl.Text = "Games"
    phsl.Name = "Games"
    pncl.Text = "Shop"
    pncl.Name = "Shop"
    pnbl.Text = "Help"
    pnbl.Name = "Help"
    LGui.Games.MouseButton1Click:Connect(function()
        LGui.Games.Text = "games"
        LGui.Games.Name = "games"
        LGui.Shop.Text = "games"
        LGui.Shop.Name = "games"
        LGui.Help.Text = "games"
        LGui.Help.Name = "games"
        print "g"
    end)
end)

OUTPUT : "g"

0
Is this in StarterGui? And under a localscript? Vxpper 101 — 4y
0
It is in a script in StarterGui Frandavi1000 15 — 4y
0
Put it in a local script mc3334 649 — 4y

1 answer

Log in to vote
0
Answered by
IDKBlox 349 Moderation Voter
4 years ago

Honestly I have no idea what you're wanting exactly... But

I can see you have a few issues... like

LGui.PHSL.MouseButton1Click:Connect(function() -- Player clicks PHSL changes the names of the leagues to games,shop,and help on the GUI and Explorer
    phsl.Text = "Games"
    phsl.Name = "Games"
    pncl.Text = "Shop"
    pncl.Name = "Shop"
    pnbl.Text = "Help"
    pnbl.Name = "Help"
    LGui.Games.MouseButton1Click:Connect(function() -- I should NOT be inside of LGui.PHSl.MouseButton1Click
        LGui.Games.Text = "games"
        LGui.Games.Name = "games"
        LGui.Shop.Text = "games"
        LGui.Shop.Name = "games"
        LGui.Help.Text = "games"
        LGui.Help.Name = "games"
        print "g"
    end)
end)

You don't want to put a function within a function... That screws up a lot lol

I have no idea how you're gui is but I set up my own gui for it and set this up for ya. But that's why you were having your issue. you should not put a functions inside of a function (at least not without a connection which you can disconnect) Example:

local button = script.Parent.Button
local button2 = script.Parent.Button2

button.MouseButton1Click:Connect(function()
    if connection == nil then
        print('Button 2 activated')
        connection = button2.MouseButton1Click:Connect(function()
            print('This can ONLY happen if button has been pressed')
            connection:Disconnect()
            connection = nil
        end)
    end
end)

print('Nothing will happen with button 2 UNLESS you press button 1 first')

I have rewrote your code below.. As I stated i'm not exactly sure what you're doing or wanting to do completely so. Yeah. Have any questions feel free to ask

Anyways, I cleaned it up a little bit and added a function that does exactly what it says It changes the name and text for the given first argument, changes it to whatever the second argument is

local LGui = script.Parent.LGui

local phsl = LGui.PHSL
local pncl = LGui.PNCL
local pnbl = LGui.PNBL
local back = LGui.back

local games = LGui.Games
local shop = LGui.Shop
local help = LGui.Help

local function changeNameAndTextFor(button,name)
    button.Name = name
    button.Text = name
end

LGui.PHSL.MouseButton1Click:Connect(function()
    changeNameAndTextFor(phsl,'Games')
    changeNameAndTextFor(pncl,'Shop')
    changeNameAndTextFor(pnbl,'Help')
end)

LGui.Games.MouseButton1Click:Connect(function()
    changeNameAndTextFor(games,'games')
    changeNameAndTextFor(shop,'games')
    changeNameAndTextFor(help,'games')
end)

Ad

Answer this question