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

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

Asked by 5 years ago

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

Log in to vote
2
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago

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.

0
No homo but I love you. Nobody seems to understand about using the deprecated connect. User#19524 175 — 5y
Ad

Answer this question