Change text to match local count

I’ve been trying to make a simple script, it should change the text of a sign to match with the number of times you have clicked a button.

I tried to make the script, but I do not know how to change the text matching the local

text = “You have clicked CurrentCount times!” --It does not detect CurrentCount as the local, because it’s between the comas and it appears as text

Here’s the script so far:

Base = game.Workspace.CountingButton.Base
ClickZone = game.Workspace.CountingButton.ClickZone
ClickDetector = game.Workspace.CountingButton.ClickZone.ClickDetector
text = game.Workspace.CountingSign.TextZone.SurfaceGui.TextLabel.Text
text = “You have clicked 0 times!”
local CurrentCount = 0

ClickDetector.MouseClick:Connect(function()

local CurrentCount = CurrentCount +1
wait(0.1)
text = “You have clicked CurrentCount times!”

end)

The result of the above is the sign starting with “You have clicked 0 times” as text, and once the button is clicked, the text changes to “You have clicked CurrentCount times!”

Searched for this but couldn’t find any awnser, thanks!

CurrentCount is a variable, so write the text like this:
“You have clicked " ..CurrentCount.. " times!”

1 Like

Now the text is a number instead of CurrentCount, yet the number is always 0, I’m not sure why as the text should update each time the button is clicked.

Though at least now I know how to add the variable into the text, thanks for that!

You keep redefining CurrentCount as a new variable, instead of updating the old one.

local Base = game.Workspace.CountingButton.Base
local ClickZone = game.Workspace.CountingButton.ClickZone
local ClickDetector = game.Workspace.CountingButton.ClickZone.ClickDetector
local CurrentCount = 0

ClickDetector.MouseClick:Connect(function()
    CurrentCount += 1 -- increment the number
    game.Workspace.CountingSign.TextZone.SurfaceGui.TextLabel.Text = string.format("You have clicked %d times!", CurrentCount)
end)
1 Like

You are redefining the variable in your mouse event. You don’t need to do that. It should just be:

CurrentCount = CurrentCount +1

This now works, thank you! Though I do not understand what does the %d mean in the script

%d is a string pattern. It represents a number, essentially

1 Like