How do I prevent the lag spike from this script?
Asked by
5 years ago Edited 5 years ago
I've already asked this question before but I had no luck with answers. So basically I've been working on this script that finds other parts named "Destroyable," and so when the "Destroyable" part is within range of the destruction part (where this script is located,) it moves it around. Now I know making parts move around will cause lag, but I'm not concerned about that. What I am concerned about, however, are the annoying lag spikes this script causes, I'm pretty sure it's because of GetChildren() and GetDescendants() but I need them to find the parts and such. If there's another way to do this without using them both please help. Thank you for reading :)
03 | local DISVALUE = script.Parent.Distance.Value |
04 | local DEVALUE = script.Parent.DE.Value |
05 | local partsW = game.Workspace:GetChildren() |
08 | if partsW [ i ] :IsA( "Model" ) and partsW ~ = script.Parent.Parent then |
09 | local partsGet = partsW [ i ] :GetDescendants() |
10 | for i = 1 ,#partsGet do |
11 | if partsGet [ i ] :IsA( "Part" ) or partsGet [ i ] :IsA( "WedgePart" ) or partsGet [ i ] :IsA( "UnionOperation" ) then |
12 | local dis = (partsGet [ i ] .Position-script.Parent.Position).Magnitude |
13 | if partsGet [ i ] .Name = = "Destroyable" then |
14 | if dis< script.Parent.Distance.Value then |
15 | local thisisaPart = partsGet [ i ] |
16 | thisisaPart.Anchored = false |
17 | thisisaPart:BreakJoints() |
18 | thisisaPart.Velocity = Vector 3. new(math.random(- 100 , 100 ),math.random( 20 , 40 ),math.random(- 100 , 100 )) |