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

Total click counter won't add?

Asked by
Omzure 94
4 years ago
Edited 4 years ago

I've been making a total click counter and for some reason when I test the game the click counter won't work and there will be an error stating

"Players.MLG_Gamer9876.PlayerGui.GUI.counter:3: attempt to perform arithmetic on field 'Text' (a string value)"

and I have no idea what that means, but here's the script...


local text = script.Parent.totalclicks script.Parent.click.MouseButton1Click:Connect(function() text.Text = "Total Clicks:"..text.Text + 1 end)

1 answer

Log in to vote
0
Answered by
memguy 161
4 years ago
Edited 4 years ago

You are trying to add a number to a string value. You have to convert the text to a number first to perform math operations. It should look like this:

local text = script.Parent.totalclicks
script.Parent.click.MouseButton1Click:Connect(function()
    text.Text = tostring(tonumber(text.Text) + 1)
end)

tonumber converts string to number and tostring does the opposite. (In this case tostring is not really necesarry. It's just for better understanding.)

0
memguy the number only goes up to 1 and then doesn't add up anymore Omzure 94 — 4y
0
oh yeah my bad memguy 161 — 4y
0
fixed it memguy 161 — 4y
Ad

Answer this question