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

This script is kind of annoying, mainly because I can't figure out how to make it a loop. Help?

Asked by 6 years ago

Hello, I am having trouble making loops. Mainly because I do not fully understand them, and how they work. Help me, please.

This is how I want the loop to be but just as a onTouched, instead of a click.

script.Parent.ClickDetector.MouseClick:Connect(function()
    for i = 0, 1, .1 do
        game.Workspace.WoodlogWB.WoodP.Transparency = 1 - i  
        wait(0.1)
    end

    game.Workspace.WoodlogWB.WoodP.CanCollide = true
end)

This is the script I want to become a loop, not infinite loop, just a loop for 10 times going down.

function onTouched()
    game.Workspace.StepBlock.White2.Transparency = 0.9
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.8   
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.7
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.6
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.5
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.4   
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.3
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.2
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0.1
    wait(0.1)
    game.Workspace.StepBlock.White2.Transparency = 0
    wait(0.1)
    game.Workspace.StepBlock.White2.CanCollide = true
end

script.Parent.Touched:Connect(onTouched)
0
Don't post about how stupid I am, just help me. Yosufgamer -7 — 6y
0
I will which script do you want looped top or bottom or both?? StateSector 8 — 6y
0
Bottom, top is already looped. Yosufgamer -7 — 6y
0
put the loop in the function smh... greatneil80 2647 — 6y
View all comments (2 more)
0
Oh ok StateSector 8 — 6y
0
you can use tweenservice for a smoother transition User#23365 30 — 6y

3 answers

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

Instead of having the long wait(1) loops, we can change it up, only taking a couple lines, which is much better, cleaner and more efficient in scripting.

A loop is just as it sounds, it plays the same thing over and over again, until either the Player stops the loop, the loop changes to something else, or until the loop has finished what it had to do.

We can use loops for many things such as

1.Stat Chaning 2.Gamemode Choosing 3.Part Moving

And much more.

You can use while loops, for loops and the classic, unefficient version of loop, wait(number).

The while and for loops are much better when coming to scripting. They're easier, cleaner and more efficient to use.

The trouble you're having with your script, is, not knowing any of these beneficial loops.

Here is your script modified.

local part = script.Parent -- Variable for the Part

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- Checking if a player has touched the brick
        while true do
            part.Transparency = part.Transparency - 0.1
            wait(0.1)
        end
            if part.Transparency == 0 then
                part.CanCollide = true
                    script:Destroy() -- Destroying Script since it will cause bugs if not.
                end
            else
        print("Humanoid not found") -- Alerting the console that a player hasn't touched the brick
    end
end

script.Parent.Touched:Connect(onTouched)

If this has helped you let me know and click the big answer button :D

0
The while loop on line 5 is an infinite loop. The code after that will never run since there is no break. There's no reason for a while loop in this type of question at all. xPolarium 1388 — 6y
0
This is an example, it depends on how he wants it to be scripted, it could be like an easteregg that could appear once. I've mentioned to him many loops so he'll be able to work out it in the end. TheOnlySmarts 233 — 6y
Ad
Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

The for-loop in the first code block is known as a numerical for-loop. This allows you to run the same code a number of times (looping the same code).

It's basic form looks like:
for startpoint = 0, endpoint, increment do
    --code
end

startpoint is a variable where the loop will start at. endpoint is a value that the loop will count towards and reach. increment is the value which the loop will add to the startpoint to get to the endpoint all while running the code. In this type of for-loop, you could utilize the startpoint variable to change properties that require these values.

If I wanted to print numbers 1-10 with 1 as an increment it would look like:
for i = 1, 10, 1 do
    print("Number: "..i)
end

--or:
for i = 1, 10 do
    print("Number: "..i)
end

--both print:
-->1
-->2
-->3

In the second example above, notice how I gave no increment of 1? That's because it isn't needed unless you specify it. If you don't provide one, the loop will have the increment be 1.

Another useful thing about numerical for-loops is being able to 'count down'. We do this by making the increment a negative value. The startpoint must also greater than the endpoint for it to reach it:

for i = 10, 1, -0.5 do
    print("Number: "..i)
end

-->10
-->9.5
-->9.0
-->...and so on

That's probably how basic I can get with numerical for-loops for you. You can see more about them on this Roblox article or this Lua manual article


Back to your question:

You already correctly use a numerical for-loop in your first code block so I'm assuming it isn't your code. The same can easily be done to the second code without needing to copy the same line of code a number of times.

local partTouch = script.Parent

local white2 = partTouch:FindFirstChild("White2")

local function onPartTouch()
    for i = 1, 0, -0.1 do
        White2.Transparency = i
        wait(.1)
    end
    White2.CanCollide = false
end

partTouch.Touched:Connect(onPartTouch)

The above makes the part White2 turn from transparent to fully visible. You should see that the numerical loop adds '-0.1' to 1 until it reaches 0 at a wait interval of .1 second.

In the code I also assume 'StepBlock' is actually script.Parent so make sure that's correctly defined.


Edit:

Added line 10 to finished code. White2 has it's collision set to false only after the transparency loop is finished.

0
Right, not mine, someone helped me with that. Also, could you add the can collide into that? Thanks. Yosufgamer -7 — 6y
0
Edited. Know that you should attempt to correct the code yourself since the point of answering is to explain why it's wrong and how to fix it. xPolarium 1388 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Basically a loop is a thing that repeats itself multiple times what it does is replicate your code and executes it through a loop which behaves exactly as you want it to

You just use script.Parent to get the wood thing and put this inside the touched thing and that's it if you're making a obby it works great for that

I'll help you just tell me which code you want it for but here's a little template

--wood is your thing as a variable it's white2 block I think

for i = 1,0,-.1 do
wood.Transparency = i
end

--this puts your thing on opaque to transparent always just minus it to make it work for any value
0
Sorry check it now got a bit mixed up StateSector 8 — 6y
0
Updated StateSector 8 — 6y
0
Okay. Yosufgamer -7 — 6y
0
Works if you have more questions ask me here StateSector 8 — 6y

Answer this question