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

Why my If statement is not working? Is it being ignored?

Asked by 5 years ago

Hey guys, sorry for asking again, but my If statement in the end is not working, what's the problem? When the var Buttonsleft == 0, it just doesn't print "All buttons done", why?

PS: Ignore the strange words like "porta", those are in portuguese... PS: I think the repeated functions can be transformed in just one, maybe that's a bad practice, but i just don't know how to do it, sorry :/

clickquantity1 = 5
clickquantity2 = 5
clickquantity3 = 5
clickquantity4 = 5
Buttonsleft = 4
porta1 = game.Workspace.Porta
porta2 = game.Workspace.Porta2
porta3 = game.Workspace.Porta3
porta4 = game.Workspace.Porta4
Table = {porta1, porta2, porta3, porta4}

function clickbutton1()
    workspace.Painel1.SurfaceGui.TextLabel.Text = clickquantity1 .. " Click's left"
    clickquantity1 = clickquantity1 - 1
    if clickquantity1 == 0 then
        workspace.Painel1.SurfaceGui.TextLabel.Text = "Finished Button"
        Buttonsleft = Buttonsleft - 1
    elseif clickquantity1 < 0 then
        workspace.Painel1.SurfaceGui.TextLabel.Text = "Buttons left: " .. Buttonsleft
    end
end

game.Workspace.Button1.ClickDetector.MouseClick:Connect(clickbutton1)

function clickbutton2()
    workspace.Painel2.SurfaceGui.TextLabel.Text = clickquantity2 .. " Click's left"
    clickquantity2 = clickquantity2 - 1
    if clickquantity2 == 0 then
        workspace.Painel2.SurfaceGui.TextLabel.Text = "Finished Button"
        Buttonsleft = Buttonsleft - 1
    elseif clickquantity2 < 0 then
        workspace.Painel2.SurfaceGui.TextLabel.Text = "Buttons left: " .. Buttonsleft
    end
end

game.Workspace.Button2.ClickDetector.MouseClick:Connect(clickbutton2)

function clickbutton3()
    workspace.Painel3.SurfaceGui.TextLabel.Text = clickquantity3 .. " Click's left"
    clickquantity3 = clickquantity3 - 1
    if clickquantity3 == 0 then
        workspace.Painel3.SurfaceGui.TextLabel.Text = "Finished Button"
        Buttonsleft = Buttonsleft - 1
    elseif clickquantity3 < 0 then
        workspace.Painel3.SurfaceGui.TextLabel.Text = "Buttons left: " .. Buttonsleft
    end
end

game.Workspace.Button3.ClickDetector.MouseClick:Connect(clickbutton3)

function clickbutton4()
    workspace.Painel4.SurfaceGui.TextLabel.Text = clickquantity4 .. " Click's left"
    clickquantity4 = clickquantity4 - 1
    if clickquantity4 == 0 then
        workspace.Painel4.SurfaceGui.TextLabel.Text = "Finished Button"
        Buttonsleft = Buttonsleft - 1
    elseif clickquantity4 < 0 then
        workspace.Painel4.SurfaceGui.TextLabel.Text = "Buttons left: " .. Buttonsleft
    end
end

game.Workspace.Button4.ClickDetector.MouseClick:Connect(clickbutton4)

if Buttonsleft == 0 then
    print("All buttons done")
end
0
the if statement is only running once, since the value of buttonsleft isnt going to initially be 0 and its never asked to run again after the value is 0, it will never print. DinozCreates 1070 — 5y
0
Hm.. I think i see it, It's like the if is seeing that Buttonsleft is not zero, and then If just do nothing, is that right? Mathzs02 14 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Lets just use this

local db = false
while Buttonsleft == 0 and db == false do
    print("All buttons done")
    db = true
end
0
I think this is a good way to do it, but something is missing, it says "attempt to index global 'Buttonsleft' (a number value)", so, should Buttonsleft be something else than a number value? Mathzs02 14 — 5y
0
you should change all of your variables into locals, local X = Y instead of just X = Y DinozCreates 1070 — 5y
0
Ok, I will try it Mathzs02 14 — 5y
0
same thing, "attempt to index local 'Buttonsleft' (a number value)" :( Mathzs02 14 — 5y
View all comments (10 more)
0
i made an uh oh. .Changed only works on variables not inside the script, try the while loop i just updated the script to. DinozCreates 1070 — 5y
0
It's saying that because obviously you're using Parts or something that isn't a number value for your Buttonsleft. But I don't know the solution to this issue, and I can't help because im so tired xD. TheOnlySmarts 233 — 5y
0
Nvm, now that Dinoz has edited his script, it looks fine and no issue to be seen. TheOnlySmarts 233 — 5y
0
no its a limitation of .Changed, i knew that i was just being too quick. DinozCreates 1070 — 5y
0
yeah xd i was going to say why are u using changed. TheOnlySmarts 233 — 5y
0
because its perfect for this situation. i dont like to use while loops unless i have to. DinozCreates 1070 — 5y
0
Compilation error: Line 2: 'do' expected near 'then'. Compilation error: Line 5: unexpected symbol near ')' AbstractionsReality 98 — 5y
0
oops. corrected. DinozCreates 1070 — 5y
0
Well, it didnt work, but thank you so much for the help guys :) Mathzs02 14 — 5y
0
I used the Changed property that you told me before in an IntValue, and it worked, thank you again hehe Mathzs02 14 — 5y
Ad

Answer this question