What do you want to achieve? When the Text of the TextLabel hits 10 after clicking it 10 times, it goes back to 0.
What is the issue? The code is not working as intended, specifically the “if…” part of it.
What solutions have you tried so far? Tried troubleshooting the issue, didn’t appear to be anything with the variables, it seemed to not just be able to detect that the Text was 10.
local arvo = script.Parent.kalikka1.SurfaceGui.TextLabel
local klikkeri = script.Parent.kalikka1.ClickDetector
script.Parent.kalikka1.ClickDetector.MouseClick:Connect(function()
arvo.Text = arvo.Text+1
end)
if arvo.Text == 10 then arvo.Text = 0
end
local arvo = script.Parent.kalikka1.SurfaceGui.TextLabel
local klikkeri = script.Parent.kalikka1.ClickDetector
klikkeri.MouseButton1Click:Connect(function()
arvo.Text == arvo.Text + 1
end)
if arvo.Text == 10 then
arvo.Text == 0
end
I tried putting it there and now the clickdetector part of the code stopped to work. Now when I click the part it no longer raises the text by 1. It just stays at 0.
The error is that you put the if block not in the mouseClick event but when the script the client loads so try this :
local arvo = script.Parent.kalikka1.SurfaceGui.TextLabel
local klikkeri = script.Parent.kalikka1.ClickDetector
script.Parent.kalikka1.ClickDetector.MouseClick:Connect(function()
arvo.Text = tostring(arvo.Text) +1
if arvo.Text == tostring(10) then
arvo.Text = "0"
end
end)
This isn’t working because .Text returns a string, numbers and strings aren’t the same so they are a false comparision.
Try this:
local arvo = script.Parent.kalikka1.SurfaceGui.TextLabel
local klikkeri = script.Parent.kalikka1.ClickDetector
script.Parent.kalikka1.ClickDetector.MouseClick:Connect(function()
-- In order to increment the text's number value we first need to make it a number
-- So we'll convert the text into a number via tonumber(arvo.Text).
-- Now we need to add 1 to that so we'll increment that value by 1.
-- Since TextLabel's use Strings for their Text value we will convert this
-- back to a string. (Roblox will actually do this for you but this helps you understand
-- what you're dealing with.
arvo.Text = tostring(tonumber(arvo.Text) + 1)
end)
-- Since .Text is a string we must compare it with another string, otherwise this statement is always false.
-- So we'll compare the text to 10 expressed as a string via the quotation marks around it.
if arvo.Text == "10" then
arvo.Text = "0"
end
Thanks for clarifying that up! However, the original problem still persists, the code won’t set it back to 0 when it hits 10, it just keeps going up from there.
local arvo = script.Parent.kalikka1.SurfaceGui.TextLabel
local klikkeri = script.Parent.kalikka1.ClickDetector
script.Parent.kalikka1.ClickDetector.MouseClick:Connect(function()
local num = tonumber(arvo.Text) +1
if num == 10 then
num = 0
end
arvo.Text = num
end)
local kalikka1 = script.Parent.kalikka1
local arvo = kalikka1.SurfaceGui.TextLabel
local clickDetector = kalikka1.ClickDetector
clickDetector.MouseClick:Connect(function()
local number = arvo.Text + 1
if number == 10 then
number = 0
end
arvo.Text = number
end)
local arvo = script.Parent.kalikka1.SurfaceGui.TextLabel
local clickDetector = script.Parent.kalikka1.ClickDetector
clickDetector.MouseClick:Connect(function()
local num = tonumber(arvo.Text)
num += 1
if num == 10 then
arvo.Text = 0
end
end)