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

How do you make a for i loop only start when a condition has been met?

I'm trying to figure how to only make the for i loop for my game start when it's met a condition. Here's my script:

if script.Parent.Parent.Visible == true then
		for i = 1, #text do
		script.Parent.Text = string.sub(text, 1, i)
		wait(0.01)
	end
end

4 comments

0

That works though? Put it in an event so it runs each time it fires.

0

Nothing is working ¯_(?)_/¯

0

string.len(text) over #text possibly?

0

try putting it in a while loop? make sure to have a wait in it so it doesn't crash studio ¯_(++)/¯

1 answer

✓ Accepted answer

The attempt you made is pretty close already. What you're missing is an event that'll fire each time script.Parent.Parent changes, so that the script checks it every time. For that we'll do the following:

script.Parent.Parent.Changed:connect(function()
	if script.Parent.Parent.Visible == true then
		-- To make sure that #text is greater than 1, I'd also wrap this inside a condition like this:
		if #text >= 1 then
			for i = 1, #text do
				script.Parent.Text = string.sub(text, 1, i)
				wait(0.01)
			end
		end
	end
end)

Hope that helps. If it doesn't, feel free to ask further questions in the comments to this.

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