Try playing the tween without creating a new thread. Tweens automatically run in their own thread by nature, thus eliminating the need for task.spawn().
Out of curiosity, why create a separate thread at all? Not sure how your gun system operates, but it would make sense to me to use an event-based approach here to determine when the bullet impacts an object.
The bullet is not a physical object, it’s just a raycast so I don’t need to use an event to detect if it impacts an object
It’s just “if RaycastResult” or “if RaycastResult.Instance”
when using task.wait(6), it would pause the entire script, so I put it in a task.spawn function
Are you tweening this on the client or server? Excessive tweening on the server can cause lag and other tween objects to slow down. Tweening objects client-side is best for performance. You also have the tween duration set to 10 by doing “TweenInfo.new(10)”
Try to identify the BulletHole texture/model once with WaitForChild and then clone the same object you’ve already identified instead of waiting for an instance every time. This may not make any difference, but it is optimal for your code. I also made a minor change to your Tween.Completed event, you can set a variable to it if you want it to yield the tween state.
local BulletHole = RS:WaitForChild("BulletHole") -- at the top of your script
local NewBulletHole = BulletHole:Clone()
NewBulletHole.CFrame = CFrame.new(BulletResult.Position, BulletResult.Position + BulletResult.Normal)
NewBulletHole.Parent = workspace
task.delay(6, function()
local BulletHoleTween = TweenService:Create(NewBulletHole.Decal, TweenInfo.new(10), {Transparency = 1})
BulletHoleTween:Play()
BulletHoleTween.Completed:Wait()
NewBulletHole:Destroy()
end)
It’s tweened on the server. It’s probably best that you mentioned this because I would imagine hundreds of bullet holes being spawned and tweened at the same time for every user in the server would be very performance heavy
Anyways, switching it to the client and I’ll let you know if it works
This actually is not necessary seeing an event connection is never established, and as soon as BulletHoleTween is out of scope, it will be cleaned up from memory. Nothing else in the code references the tween object, so therefor it becomes eligible to get collected by the garbage collector.
I just found the issue after about 2-3 hours of debugging (and googling), somehow there was a humanoid in the workspace which basically made every single tween lag super badly