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

Help fixing a script that changes transparency with functions?

Asked by 7 years ago
Edited 7 years ago

This script should: 1) Start with all Parts (1-9) with Transparency 1

2) Turn each part to Transparency 0, with a 1 second delay after each one

3) onTouch by a humanoid, if Transparency is 0, then it kills humanoid

4) I want them all to disappear at the same time

Here is what I have so far (Not Working) I have a model, inside the model is the script along with all the parts.

I know probably the "while true do" is in the wrong spot, along with the "end"s and "else"s. Need help figuring out where to put those.

while true do
script.Parent.Part1.Transparency = 1
wait(1)
script.Parent.Part1.Transparency = 0
 function onTouch(part)
    if script.Parent.Part1.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part2.Transparency = 1
wait(1)
script.Parent.Part2.Transparency = 0
        if script.Parent.Part2.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part3.Transparency = 1
wait(1)
script.Parent.Part3.Transparency = 0
            if script.Parent.Part3.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part4.Transparency = 1
wait(1)
script.Parent.Part4.Transparency = 0
            if script.Parent.Part4.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part5.Transparency = 1
wait(1)
script.Parent.Part5.Transparency = 0
         if script.Parent.Part5.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part6.Transparency = 1
wait(1)
script.Parent.Part6.Transparency = 0
            if script.Parent.Part6.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part7.Transparency = 1
wait(1)
script.Parent.Part7.Transparency = 0
            if script.Parent.Part7.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part8.Transparency = 1
wait(1)
script.Parent.Part8.Transparency = 0
            if script.Parent.Part8.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
end
else
script.Parent.Part9.Transparency = 1
wait(1)
script.Parent.Part9.Transparency = 0
            if script.Parent.Part9.Transparency == 0 then
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if humanoid ~= nil then 
        humanoid.Health = 0
    end
    else
        wait()
    end
    end

script.Parent.Touched:Connect(onTouch)
wait(3)
end

1 answer

Log in to vote
0
Answered by
Jellyfosh 125
7 years ago
Edited 7 years ago

Your code is very unnecessarily lengthy because you go in and individually write out code for each part. Definite props for trying, your code is actually fairly close to being usable. However, an incredibly helpful tool to use to avoid this unneeded length is For Loops. There are many different ways to utilize this type of looping, in the code below I have used one of the most common/useful. I'll explain what I've written and how it works.

local model = script.Parent

for i = 1,9,1 do 
    local part = model:FindFirstChild("Part"..tostring(i))
    part.Touched:Connect(function(whatTouched)
        local humanoid = whatTouched.Parent.Humanoid
        if part.Transparency == 0 and humanoid then
            humanoid.Health = 0
        end
    end)
end

while wait() do 
    for i = 1,9,1 do
        local part = model:FindFirstChild("Part"..tostring(i))
        part.Transparency = 1
        wait(1)
        part.Transparency = 0
    end
end

The statement "for i = 1,9,1" basically can be read as "loop variable(i) from 1 to 9 by increments of 1. This means that the loop will run through 9 times in total, and in each separate loop the variable "i" will be equal to 1,2,3,4,5,6,7,8 and 9. I first use this to set up the "touched" function for each individual part as seen in the first group of the script. For each loop, I redefine local part to equal the first child of your model that has the name "Part"..tostring(i).

Note that "i" is an integer, and so you need to use tostring(i) to make it a string. The two dots following "Part" mean that "Part" and the string form of the loops number(i) will be read by the script as 1 string ("PartNUMBER").

After I have set up the variable for the part, I can give it the touched function, which will return "whatTouched" it which will be useful for finding if whatever touched it can die. I define humanoid as "whatTouched.Parent.Humanoid". This may not be the case, it is possible that a random part in your game could have touched these parts, and so in the following line I make two checks: "is the part transparent?" and does "humanoid" exist?. This sets the code up to find a humanoid and kill it, if it exists.

Similarly, in the second chunk of code, I use "while wait() do" because "while true do" is somewhat deprecated and should no longer be used anymore. I go through a similar process using a for loop to define a different part for each loop, and in between each loop changing transparency and waiting a second. This for loop will play forever because it is inside of while wait() do.

0
Thank you for the good answer & explanation! I can use this for something else i'm making, but for this question it doesn't satisfy point #4 above. How would I edit it to have them disappear at once? Opposed to disappearing one at a time. ElectronicMask 4 — 7y
0
Using "for i =1,9,1 do" without a wait() time will simply go through the list but at the same time. Like in the first chunk of code, each of the 9 parts there gain that function at the same time because that chunk doesn't have a wait time. Jellyfosh 125 — 7y
Ad

Answer this question