A while ago I made a little gun system, adding a few features to it. Eventually it became so complicated that it actually started to lag people. I decided to restart from scratch. I can do extremely basic optimizing, but it obviously wasn't enough. Now the performance is worse than my older gun system. Simply looking up and down raises the script's activity to a sudden 15-20% I used the same method in my older gun system (other than having the function not be inside the Equipped function) and it was much less activity. How would I go optimizing code better? Here's some basics I know:
--Unoptimized LocalScript Code:
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.1, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.2, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.3, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.4, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.5, 0)
wait()
--Optimized LocalScript Code:
local LHip = game.Players.LocalPlayer.Character.Torso["Left Hip"]
local CF = CFrame.new
for i = 0.1, 0.5, 0.1 do
LHip.C1 = CF(0, i, 0)
wait()
end
I found this little code snippet on ROBLOX's blog about optimizing. However, the code in the picture doesn't work. The script simply errors and stops working. How can I optimize my code (better)?

1 comment
I see... That's really helpful. Thanks adark!