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)
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.
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?
Whoops, I overlooked this detail and never actually used this in real practice before.
Apologies for the confusion, I hope you were able to solve your issue!