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

Does anyone know how I can simplify this repeating script?

Asked by
zomspi 541 Moderation Voter
4 years ago

Pretty much the title.



game:WaitForChild("Players").PlayerAdded:Connect(function() wait(5) game.Workspace.HouseWall1.Transparency = 0.9 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.8 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.7 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.6 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.5 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.4 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.3 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.2 wait(0.1) game.Workspace.HouseWall1.Transparency = 0.1 wait(0.1) game.Workspace.HouseWall1.Transparency = 0 end) game:WaitForChild("Players").PlayerAdded:Connect(function() wait(7) game.Workspace.HouseWall2.Transparency = 0.9 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.8 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.7 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.6 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.5 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.4 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.3 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.2 wait(0.1) game.Workspace.HouseWall2.Transparency = 0.1 wait(0.1) game.Workspace.HouseWall2.Transparency = 0 end)

2 answers

Log in to vote
1
Answered by
megukoo 877 Moderation Voter
4 years ago
Edited 4 years ago

Great that you've acknowledged this kind of code structure. This code can easily be substituted using a numeric for loop.

For loops work generally in this format:

for i = start, end, increment do
    -- i will hold the number of every loop.
    -- start and end define the start and end numbers.
    -- increment defines the increment of the loop. this can be positive or negative (counting up and down respectively.)
end

You may be confused on how to incorporate this at first, but it's simple once you look at it. Your Part's Transparency starts at 1.

We want to decrease the Transparency by 0.1 while waiting 0.1 seconds between each step, and finally end at a Transparency of 0.

Here's a code sample to match the description.

local houseWall2 = game.Workspace.HouseWall2

for i = 1, 0, -0.1 do
    houseWall2.Transparency = i 
    -- remember that "i" is the number for each iteration. for the first loop around, it'll be 0.9. then 0.8. then 0.7. all the way down to 0, our ending number. we decrease by 0.1 as marked by our -0.1.
    wait(0.1)
end

You can see this results in the same desired effect, as well as being shorter and to the point as well.

Hopefully this helps you!

Ad
Log in to vote
1
Answered by
cfiredog 274 Moderation Voter
4 years ago

A numerical for loop is great for repeating a block of code. It has the following syntax:

for var = start, limit, step do
    -- code block
end

The code block is repeated until the value of var passes the value limit. The value of var is incremented by the value step every time the code block is executed. If the third expression (step) is absent, then a step of 1 is used.

To expand on this, we can use multithreading to simultaneously change the transparency of a model of parts (the walls of a house in this case).

local walls = workspace:WaitForChild("Walls")

-- Change the transparency of each wall simultaneously
for _,wall in pairs(walls:GetChildren()) do
    spawn(function()
        for i = 0, 1, 0.01 do
            wall.Transparency = i
            wait()
        end
    end)
end

This is a bit more involved, but the general concept still stands.

Answer this question