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

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

Asked by 5 years ago
Edited 5 years ago

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

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

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!

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. DinozCreates 1070 — 5y
1
Great Neil basically has the gist of it Synth_o 136 — 5y
1
Just take what greatness typed and change it Synth_o 136 — 5y
1
shouldn't be to hard Synth_o 136 — 5y
View all comments (5 more)
1
great Neil basically got the main part of it Synth_o 136 — 5y
1
Sorry for splitting the comment my computer is really buggy rn Synth_o 136 — 5y
0
Alright I'll edit it greatneil80 2647 — 5y
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. DinozCreates 1070 — 5y
0
Alright! You are welcome. If you need any help with anything else, ask me :) greatneil80 2647 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Nice post! This is good too, I don't really work with tween info though. greatneil80 2647 — 5y
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. DinozCreates 1070 — 5y
0
No problem, you can really just change the Transparency = 0 to Transparency = 1 theking48989987 2147 — 5y
0
and vice versa theking48989987 2147 — 5y
View all comments (3 more)
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? DinozCreates 1070 — 5y
0
This is a good post! Synth_o 136 — 5y
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. :) DinozCreates 1070 — 5y

Answer this question