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

For loop is not looping? How to loop until goal achieved

The goal of this script is to continuously run in a loop, always hitting the "else" statement until it eventually turns "Visible" and then it would end the loop.

Clearly that's not whats happening! Currently when running it does print "Invisible" once then stops, meaning its not looping. I believe my error lies in the "until found" line, but I'm unsure of how to run this the correct way.

For backstory this is a tycoon and i need this to activate once an object is purchased and moved into the referenced location with referenced name.

local found = script.Parent.Parent.Parent.Parent.PurchasedObjects:FindFirstChild("FirstFloorStairs")

if found then
	print("Visible")
	script.Parent.Transparency = .5
else
	print("Invisible")
	script.Parent.Transparency = 1
repeat
until found
end

Any pointers or help is much appreciated, I'm very new to the scene :).

EDIT

	local list = script.Parent.Parent.Parent.Parent.PurchasedObjects:GetChildren() -- get the list of items in a loop
	for _, v in pairs(list) do
	    if v.Name == "FirstFloorStairs" then 
	        script.Parent.Transparency = .5 -- visible!!
			print("Visible")
	    end
	end 
	local ts = game:GetService("TweenService") --tween service
	local list = script.Parent.Parent.Parent.Parent.PurchasedObjects:GetChildren() --just a list
	local tinfo = TweenInfo.new(
	    0.7,--time it takes to tween
	    Enum.EasingStyle.Sine,--the easing style(mainly for looks)
	    Enum.EasingDirection.Out,--again for aestetics
	    0,--times it repeats
	    false,--whether it reverses
	    0-- delay between repeats
	)--tween info
	 
	local properties = {Transparency = .5}
	 
	for _, v in pairs(list) do
	    if v.Name == "FirstFloorStairs" then -- if the item is a part
	        script.Parent.Transparency = 1 -- make the blocks invsible
	        local tween = ts:Create(script.Parent,tinfo,properties)--creates a tween
	        tween:Play()
	    end
	end -- create a loop

2 answers

✓ Accepted answer

So basically you want a fading block, and when the fading ends it should stop the loop? EASY!!!

So I am assuming you wanna make everything in a model transparent then change the transparency to + .01

change list to your model, don't remove GetChildren()

local list = script.Parent:GetChildren() -- get the list of items in a loop
local counter -- just set up a little counter
for _, v in pairs(list) do
	if v.ClassName = "Part" then -- if the item is a part then...
		v.Transparency = 1 -- invisible!!

		-- now for the visibility code!!
		counter = 100
		repeat wait(.001)
			counter = counter - 1
			v.Transparency = v.Transparency - 0.01
		until counter < 1
	end
end -- create a loop

This code above does the fading effects, I havent tested it in studio but, this should probably work!

If you want another part to exist and then make another part appear??? then do this: EVEN EASIER XDDD

local part_To_Exist = workspace:WaitForChild("Item") -- use wait for child to wait until that item exists!!
local part_to_change = script.Parent -- change the transparency of the part that is suppose to already exist
repeat wait() until part_To_Exist
part_to_change.Transparency = 0

the transparency of the part changes when the exist part is created!!

Tell me if you are using a model!

9 comments

1

Sorry i think i explained the issue incorrectly! My goal is to make a specific part VISIBLE based on the existence of another part. I've editted what you gave me in a failed attempt to make that work. Ill edit to show you.

1

Great Neil basically has the gist of it

1

Just take what greatness typed and change it

1

shouldn't be to hard

1

great Neil basically got the main part of it

1

Sorry for splitting the comment my computer is really buggy rn

0

Alright I'll edit it

0

You’re a king among men! Thank you Neil! I am using a model, but I should hopefully be able to figure out the necessary changes for that.

0

Alright! You are welcome. If you need any help with anything else, ask me :)

Yes, I think greatneil80's solution works, but I think it is a bit more practical to tween the transparency of the parts

local ts = game:GetService("TweenService") --tween service
local list = script.Parent:GetChildren() --just a list
local tinfo = TweenInfo.new(
	0.7,--time it takes to tween
	Enum.EasingStyle.Sine,--the easing style(mainly for looks) 
	Enum.EasingDirection.Out,--again for aestetics
	0,--times it repeats
	false,--whether it reverses
	0-- delay between repeats 
)--tween info

local properties = {Transparency = 1}

for _, v in pairs(list) do
	if v.ClassName = "Part" then -- if the item is a part
		v.Transparency = 0 -- make the blocks invsible
		local tween = ts:Create(v,tinfo,properties)--creates a tween
		tween:Play()
	end
end -- create a loop

That is the general basics on how to use tweens, you can play around a bit with the easing styles, the easing directions, and other aspects of the tween info and part properties

7 comments

0

Nice post! This is good too, I don't really work with tween info though.

0

This looks fancy! Im sorry i must've explained it so poorly i confused both of you! I made an edit to the main post with neils code that may better show what i meant.

0

No problem, you can really just change the Transparency = 0 to Transparency = 1

0

and vice versa

0

i editted the script down without the counter or the fading, and entered the information for my circumstance, with no luck. Would you be able to look at the code i posted and see if you can find whats wrong?

0

This is a good post!

0

Thanks for the code king! It wasn’t exactly what I needed but it’s very useful information, I’ll keep it tucked away for later use. :)

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