Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

CFrame movement becomes very slow / laggy after a while, how do i fix this?

the CFrame movement of the attack becomes very slow overtime, any idea how to fix this and why this is happening?


local attack = script.Parent


while true do
    wait()
    if attack.Parent == workspace then

        repeat 

            attack.Position = Vector3.new(attack.Position.X - 1, attack.Position.Y, attack.Position.Z)
            wait(0.01)
        until attack.Position.X < 2625



        attack:Destroy()
        wait(.2)



    end


end

2 answers

You're probably taking up too much memory by repeating when unnecessary to do so. Try this code and let me know if it works!

local attack = script.Parent


while true do

	if attack.Parent == workspace then

		if not (attack.Position.X < 2625) then
			
			repeat 

				attack.Position = Vector3.new(attack.Position.X - 1, attack.Position.Y, attack.Position.Z)
				wait()
				
			until attack.Position.X < 2625

			attack:Destroy()
			wait(0.2)
			
		end

	end

	wait()
	
end

2 comments

0

Nope, it still doesn't work.

0

Tip: wait() and wait(0) and wait(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 use wait() (as you've done).

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.