Tweening causing terrible lag

image
The bldtween gets created too many times, but if i only create it once it wont apply to the current projectile object.
Btw, i’m using fastcast, and the projectile is the cosmetic projectile, why im weening the maxlength is because the trail wont retract when i need it to, and that is the best way to make it happen. (tweening)

cast.LengthChanged:Connect(function(Cast, LastPoint, Direction, Length, Velocity, Projectile)
	if Projectile then 
		local ProjectileLength = Projectile.Size.Z/2
		local Offset = CFrame.new(0, 0, -(Length - ProjectileLength))
		Projectile.CFrame = CFrame.lookAt(LastPoint, LastPoint + Direction):ToWorldSpace(Offset)
		cast.RayHit:Connect(function(cast, raycastResult)
			local bldtween = game.TweenService:Create(Projectile.Trail,TweenInfo.new(0.3),{
				MaxLength = 0
			}):Play()
			wait(0.3)
			Projectile:Destroy()
			bldtween:Destroy()
		end)
	end
end)

Even though im destroying the tween, there seems to be terrible lag. Let me demonstrate:
The script is a local script.
https://gyazo.com/11a325f0cf8f1f99c28f1e9219d134c3

1 Like

You’re listening to RayHit every single time the length changes, resulting in duplicate listeners, and you just need to move the cast.RayHit listener outside of the LengthChanged event.

1 Like

There are several issues going on here so lets deconstruct the problem first.

1.) TweenBase Instances can NOT be destroyed by calling :Destroy() on them. This would just cause an error and could possibly be one of the reasons you get lag. In order to properly destroy a TweenBase, you must use the :Cancel() function on it.

2.) This issue sort of splits into 2 problems. This isn’t really an “issue” but you should definetely stick to using task.wait() instead of wait() (read more here). The other issue would be that you dont really need to wait for your tween to finish using wait functions! TweenBase objects already come with this feature and can simply be called as bldtween.Completed:Wait()!

3.) There isn’t enough information here to really “solve” your issue here. I’m not really sure what your cast variable is and stands for/does. Would you be able to go more in depth with this?

but then how am i supposed to access the projectile? i dont think that would work

yeah, im slowly moving over to task, im just used to wait() so thats why i forget.

False, TweenBases can be destroyed and is used to clear up memory, as TweenBases are an Instance.

Check the FastCast documentation, the RayHit function returns the projectile in the 4th parameter. You could then use that Instance as your bullet.

1 Like

yup, i found that right after i replied, thanks for the help.

1 Like

Whoops, I overlooked this detail and never actually used this in real practice before. :sweat_smile:
Apologies for the confusion, I hope you were able to solve your issue!

1 Like