We use cookies on this site to enhance your user experience

Oct 01 2018, 2:23 AM PST

This code sample allows a player to click a button which keeps track of how many times it has been clicked.

-- Place this code in a LocalScript in a TextButton
local textButton = script.Parent

-- Initialize the state of the button
local counter = 0
textButton.Text = "Click me!"

-- Here, we define a function that increments the counter
-- and updates the button text after doing so
local function onActivated()
	counter = counter + 1
	textButton.Text = "Clicks: " .. counter

-- Connect the "Activated" event to our function