The error that you've cited in your original post isn't present in the script segment that you've posted, though there are two issues that you'll run into with that segment despite this.
As it currently stands, you wouldn't see a change. This is because you're editing the GUI that's in StarterGui, rather than the GUI that's in the player's PlayerGui. This means that in order for a player to see the change they would currently need to reset.
You also had a second error. When the user was "closing" the GUI the button's text was changed to "BasicMath". This meant that the GUI could never be "opened" again as the if statement is checking whether the button's text is equal to "Basic Math", not "BasicMath".
Note: Both of the below assume you're doing this from a LocalScript.
local Player = game.Players.LocalPlayer
local BasicMath = Player.PlayerGui.BasicMath
local Button = Player.PlayerGui.Buttons.Frame.BasicMath
Button.MouseButton1Down:connect(function()
if Button.Text == "Basic Math" then
BasicMath.Frame.Visible = true
Button.Text = "Close"
else
BasicMath.Frame.Visible = false
Button.Text = "Basic Math"
end
end)
Though, you could also condense your script a bit by using a ternary operator and a not operator (to "toggle" visibility).
local Player = game.Players.LocalPlayer
local BasicMath = Player.PlayerGui.BasicMath
local Button = Player.PlayerGui.Buttons.Frame.BasicMath
Button.MouseButton1Down:connect(function()
Button.Text = Button.Text == "Basic Math" and "Close" or "Basic Math"
BasicMath.Frame.Visible = not BasicMath.Frame.Visible
end)
1 comment
Where's global 'we'?