This script isn't the problem (though it could trigger the problem). You have a bunch of while loops, but you don't use any events, nor do you create any threads/coroutines, so this script has no opportunity to have hundreds or thousands of threads. Since you're only executing one major command (and a few if statements) after each wait, there's no chance this is the true source of the lag.
However, since you're modifying the Position, I can imagine a few things that might trigger lag:
- This script is being cloned by other scripts
- Another script is doing something every time the position of
Attack changes
- You have a very large number of parts all in the same area (lots of overlapping) (I doubt this is your problem)
To further diagnose the problem:
- Disable just this script. Does the problem still occur? If so, it has absolutely nothing to do with this script. If the problem isn't there, investigate the problems above.
- Look for other scripts that :Clone this script, toggle the
Enabled property repeatedly (though I'm pretty sure that a script that's been Disabled has its coroutines stop resuming and so this shouldn't be an issue), or that listen to some sort of Changed event (in particular a :GetPropertyChangedSignal("Position") or "CFrame")
- Open up the Script Performance window, cause the lag to occur, and see if it reports any particular script as having really high activity.
2 comments
Nope, it still doesn't work.
Tip:
wait()andwait(0)andwait(0.01)all do the same thing (which is wait for approximately 0.033 seconds, though it can be a bit lower/higher) -- it's best to just usewait()(as you've done).