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)
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
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).
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.
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
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.
Added line 10 to finished code. White2 has it's collision set to false only after the transparency loop is finished.
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