Does anyone know why this is super laggy?

Basically, I made this localscript using TweenService, and the fps drops a lot when it is activated.

local ts = game:GetService("TweenService")


rep.Instruction2.OnClientEvent:Connect(function(hrp, place, lv)
	local part = Instance.new("Part")
	part.Color = Color3.new(1, 1, 0)
	part.Anchored = true
	part.Size = Vector3.new(0.1,0.1,0.2)
	part.Parent = game.Workspace
	part.Transparency = 0.5
	part.Rotation = lv
	part.Position = hrp
	local goal = {}
	goal.Position = place

	local tweenInfo = TweenInfo.new(0.01, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 1, false, 0)

	local tween = ts:Create(part, tweenInfo, goal)

	tween:Play()
	task.wait(0.01)
	part.Transparency = 1
	part:Destroy()
end) 

(for anyone wondering, it needs to be fast as it is a bullet)

1 Like

It could be because the function is firing multiple times at once. This script may fix it if that is the problem:

local ts = game:GetService("TweenService")
local run = false

rep.Instruction2.OnClientEvent:Connect(function(hrp, place, lv)
    if run == false then
    run = true
    wait(0.1)
	local part = Instance.new("Part")
	part.Color = Color3.new(1, 1, 0)
	part.Anchored = true
	part.Size = Vector3.new(0.1,0.1,0.2)
	part.Parent = game.Workspace
	part.Transparency = 0.5
	part.Rotation = lv
	part.Position = hrp
	local goal = {}
	goal.Position = place

	local tweenInfo = TweenInfo.new(0.01, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 1, false, 0)

	local tween = ts:Create(part, tweenInfo, goal)

	tween:Play()
	task.wait(0.01)
	part.Transparency = 1
	part:Destroy()
    run = false
end)
2 Likes

This improved it, but only slightly. I’m going to take a look at it in a bit