Why is this script causing too much lag?
I've made this script, it is scaling a huge box at size about 500x500x500 out of 6 Parts, which kill you on touch. Even though the script works properly, it causes a huge amount of lag. When I checked the Script Performance, it was taking up about 80. Here it is:
01 | Shrinker = script.Parent |
04 | ColorTransmissionTime = 5 |
05 | FinalTransparency = 0.2 |
06 | Size = Shrinker.Part.Size.X |
08 | for i, v in pairs (Shrinker:GetChildren()) do |
12 | v.Touched:connect( function (Hit) |
13 | if Hit:IsA( "Part" ) then |
14 | if Hit.Parent:FindFirstChild( "Humanoid" ) then |
16 | Hit.Parent.Humanoid.Health = 0 |
18 | local Explosion = Instance.new( "Explosion" ) |
19 | Explosion.Position = Hit.Position |
20 | Explosion.DestroyJointRadiusPercent = 0 |
21 | Explosion.Parent = game.Workspace |
28 | for i = 1 , 100 -FinalTransparency* 100 do |
29 | v.Transparency = v.Transparency - 0.01 |
30 | wait(ColorTransmissionTime/( 100 -FinalTransparency* 100 )) |
33 | for i = 1 , Size/StepSize/ 2 do |
34 | local targetCFrame = v.CFrame + (v.CFrame.lookVector * StepSize) |
35 | v.Size = (v.Size - Vector 3. new(StepSize* 2 ,StepSize* 2 ,StepSize* 2 )) |
36 | v.CFrame = targetCFrame |
37 | wait (Lenght/(Size/StepSize/ 2 )) |
Thanks for trying to help! ;)