Reputation: 3
I'm a scripting newbie and I'm trying to make a button that when you click it disappears I have a local script as the child of a text button, this is the code I'm using.
local button = script.Parent
local function onButtonActivated()
print("Button activated!")
game.StarterGui.ScreenGui.TextButton.Transparency = 1
end
How do I make it so that the computer does the function when the button is clicked?
Upvotes: 0
Views: 963
Reputation: 1
maybe use this:
local button = script.Parent
local function onButtonActivated()
button.Transparency = 1
-- or
local player = game.Players.LocalPlayer
local btn = player.PlayerGui.ScreenGui.TextButton
btn.Transparency = 1
end
button.Activated:Connect(onButtonActivated)
-- Hope it helped!
Upvotes: 0
Reputation: 110
There are a few ways to check if a button is clicked. The main way is UIButton.Activated. It works the exact same as MouseButton1Click.
Something around the lines of:
button.Activated:Connect(onButtonActivated);
Upvotes: 0
Reputation: 11
try this first make all the onButtonActivated then instead of transperency use:
button.Visible = false
it works for me
Upvotes: 0
Reputation: 7188
Check out the docs for TextButtons.
You simply need to connect your code to the Activated signal of the button.
button.Activated:Connect(onButtonActivated)
On a separate note, there's an issue with your function as well. You are modifying the button template that is in StarterGui, not the one that the player sees. UI elements are copied from StarterGui into each player's PlayerGui when the Player spawns. To access the actual button you are trying to turn invisible, you can use relative paths, like how you defined the button variable, or give the full path to the button.
local button = script.Parent
local function onButtonActivated()
button.Transparency = 1
-- or
local player = game.Players.LocalPlayer
local btn = player.PlayerGui.ScreenGui.TextButton
btn.Transparency = 1
end
button.Activated:Connect(onButtonActivated)
Upvotes: 0