Need help with the if statement

  1. What do you want to achieve? When the Text of the TextLabel hits 10 after clicking it 10 times, it goes back to 0.

  2. What is the issue? The code is not working as intended, specifically the “if…” part of it.

  3. 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
2 Likes

Try this

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
1 Like

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)


The if part does not work, it’s supposed to change the Text of the TextLabel when it goes to 10, and make it 0.

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)
1 Like

Oh right, that’s because we need to move the if statement up into the function as @ancadejo10 suggested above. Missed that one.

1 Like

you don’t need tonumber() for this

print("2" + 2)
new code
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)

I also changed the variables a bit

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)

Replied to the wrong user.

only thing I would change here is the variables
I would make script.Parent.kalikka1 a variable
you don’t have to do this, but I think it looks better