Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

0

Thank you so much. I'll go to my computer and studio and try it out right now.

This discussion is preserved as it was. New answers, comments, and votes are closed.