What can I do to make for loops less laggy?

I’m trying to make some sorta hands attack, but when I put a task.wait() within the for loop it lags my server.

please note that it’s serversided, the attack is on the server.
The hitbox is on the server and I have no plans to put it on client.
https://gyazo.com/1243827ebaacb99433905786010a7b87

fxmod.CreateSound(HRP,"rbxassetid://1880899296",5,10000,0)
for i = 0,14 do
    local NewHands = rs.Hand:Clone()
    local Position = Param1*CFrame.new(Vector3.new(0,-10,-(3*i)),NewHands.Position + Param1.LookVector)*CFrame.Angles(0,math.rad(180),math.rad(180))
    NewHands.CFrame = Position
    NewHands.Parent = workspace

    local DamageTable = {}
    debris:AddItem(NewHands,2.5)

    task.spawn(function()
        local tween = ts:Create(NewHands,TweenInfo.new(0.75,Enum.EasingStyle.Quart,Enum.EasingDirection.InOut,0,false,i/20 --[[using this was my solution to this problem, however it still lags momentarily.]]),{CFrame = NewHands.CFrame + Vector3.new(0,11,0)})
        tween:Play()

        tween.Completed:Wait()
        task.wait(0.3)
        local tween2 = ts:Create(NewHands,TweenInfo.new(1,Enum.EasingStyle.Quart),{CFrame = NewHands.CFrame - Vector3.new(0,11,0), Transparency = 1})
        tween2:Play()
    end)

    task.spawn(function()
        task.wait(2.5)
        c:Disconnect()
    end)

    task.wait(0.05)
end

Something like this? I don’t have the info to replicate it and test it myself, but using a dummy part it behaved as intended.

fxmod.CreateSound(HRP,"rbxassetid://1880899296",5,10000,0)
--creating a reference for the tweeninfo here saves on math constructing them each loop
local tweenThrow = TweenInfo.new(0.75, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)
local tweenDrop = TweenInfo.new(1, Enum.EasingStyle.Quart)

for i = 0,14 do
	local completed--declare the event connection before assigning so that it can be referenced inside of the event itself
	
	local NewHands = rs.Hand:Clone()
	NewHands.CFrame = Param1*CFrame.new(Vector3.new(0,-10,-(3*i)),NewHands.Position + Param1.LookVector)*CFrame.Angles(0,math.rad(180),math.rad(180))
	NewHands.Parent = workspace
	
	local throw = ts:Create(NewHands, tweenThrow, {CFrame = NewHands.CFrame + Vector3.new(0,11,0)})
	
	--listen for the first tween to finish and play the second
	completed = throw.Completed:Connect(function()
		--don't need variable reference for this tween, so we can call play on the constructor itself
		ts:Create(NewHands, tweenDrop, {CFrame = NewHands.CFrame - Vector3.new(0,11,0), Transparency = 1}):Play()
		
		task.wait(0.75)--waiting in here won't yield outside of this function
		
		NewHands:Destroy()--destroy hand instead of debris service
		completed:Disconnect()--disconnect event (free memory)
	end)
	
	--call the play after having declared the event listening for its completion
	throw:Play()
	
	task.wait(0.05)
end

I’m not sure what c or DamageTable were doing so I omitted them from the code.

1 Like

thanks! I already fixed the problem since somewhat the mesh I was using was extremely laggy, this will give me even more performance though

1 Like