Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a number rise each time a button is clicked?

I'm trying to make a surface GUI cookie clicker that keeps track of the number of times the cookie is clicked, but it says at zero, and I do not know about using number values, so thank you SO much if you can help.

local text = script.Parent.Parent.Clicks
local cookie = script.Parent
local value = 0

text.Text = value

cookie.MouseButton1Down:connect(function()
	value = (value+1)
end)

I am a bit of a noob at scripting (this obviously shows it) so please go easy on me!

1 answer

✓ Accepted answer

You should use a LocalScript in game.StarterPlayer.StarterPlayerScripts for this (and tick off the ScreenGui.ResetOnSpawn property so it is false).

Note that you're only setting text.Text once, to whatever value was at the time the code ran (which is 0). I've also taken the liberty of replacing the GuiButton.MouseButton1Down event with GuiButton.MouseButton1Click, which seems to be what you want (a full click is a mouse down and up). Instead, we can modify the text directly in the event listener:

local cookieDisplay = game.Workspace.ButtonDisplay.SurfaceGui.Display
local clicks = 0

cookieDisplay.MouseButton1Down:Connect(function()
	clicks = clicks + 1
	cookieDisplay.Text = "Clicks: " .. clicks
end)

RBXScriptSignal:connect() with a lowercase letter C is deprecated, you should switch to using RBXScriptSignal:Connect() with a capital letter C.

1 comment

0

No homo but I love you. Nobody seems to understand about using the deprecated connect.

This discussion is preserved as it was. New answers, comments, and votes are closed.