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

Making a loop loop until a value reaches a certain value and then breaks? (I can't figure it out!!)

Asked by
Xoqex 75
9 years ago

Alright, well I got stuck and have no clue what to do now but ask you guys. Please give some detail on your answer so I can understand it.

Here is something I thought would work but it doesn't work

SP = game.Workspace.SP
function onClicked()
while wait(0.5) do
SP.Transparency = SP.Transparency-0.1
if SP.Transparency ==0.3 then break
end
end--pretty sure this is enough ends but I know for a fact I had enough ends in studio cause nothing was erroring
script.Parent.ClickDetector.MouseClick:connect(onClicked)

So can you guys help point out flaws or give me a new script?

PS. I also tried doing


SP = game.Workspace.SP function onClicked() repeat SP.Transparency = SP.Transparency-0.1 until SP.Transparency ==0.3 end script.Parent.ClickDetector.MouseClick:connect(onClicked)

None of these work correctly I've tried all the type of loops and they all do what I want backwards or don't loop. any help is GREATLY appreciated

1 answer

Log in to vote
1
Answered by
tambre2 25
9 years ago

This should work:

SP = script.Parent

function onClicked()
    while wait(0.5) do
        if SP.Transparency <= 0.3 then
            break
        else
            SP.Transparency = SP.Transparency - 0.1
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I fixed a logic bug and also formatted your code a little bit. I assumed the script is in the SP part, so it will only work in there.

NOTE: You were missing one end and I'm assuming Studio didn't tell you that because break was on the same line as if.

0
Thank you so much. I'll go to my computer and studio and try it out right now. Xoqex 75 — 9y
Ad

Answer this question