Answered by
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.
01 | local model = script.Parent |
04 | local part = model:FindFirstChild( "Part" .. tostring (i)) |
05 | part.Touched:Connect( function (whatTouched) |
06 | local humanoid = whatTouched.Parent.Humanoid |
07 | if part.Transparency = = 0 and humanoid then |
15 | local part = model:FindFirstChild( "Part" .. tostring (i)) |
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.