Game lagging when tween plays

So, I am trying to make a JJBA game, and I created a stand summoning script.

I tested the summoning out, but for some reason, the entire game lags whenever I summon it.
Here’s a video:


You might not be able to see the lag very well in the video, but it lags a lot more than what it looks like in the video.

I searched everywhere on YouTube, DevForum, and Google for how to fix it, but I couldn’t find a solution. I tried to change some things like the tween speed and more, but nothing worked.

Here’s the code:

		        for _, part in pairs(stand:GetChildren()) do
			        if part:IsA("BasePart") and part ~= standhumRP then
				        local goal = {}
				        goal.Transparency = 0
				        local info = TweenInfo.new(.5)
				        local tween = TweenService:Create(part,info,goal)
				        tween:Play()
				        tween:Destroy()
			        end
		        end
		
				local goal = {}
				goal.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
				goal.C1 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame * CFrame.new(-3,1,2))
		        local info = TweenInfo.new(.5)
				local tween = TweenService:Create(weld,info,goal)
		        tween:Play()
	        	tween:Destroy()
			else
		        local stand = char:FindFirstChild("StarPlatinumStand")
		if stand then
			local controller = stand.PrimaryPart:FindFirstChild("Controller")
			if controller then
				
				local standhumRP = stand:FindFirstChild("HumanoidRootPart")
				
				for _, part in pairs(stand:GetChildren()) do
					if part:IsA("BasePart") and part ~= standhumRP then
						local goal = {}
						goal.Transparency = 0
						local info = TweenInfo.new(.5)
						local tween = TweenService:Create(part,info,goal)
						tween:Play()
						tween:Destroy()
					end
				end
				
				local goal = {}
				goal.C0 = controller.Part0.CFrame:ToObjectSpace(controller.Part1.CFrame)
				goal.C1 = controller.Part0.CFrame:ToObjectSpace(controller.Part1.CFrame * CFrame.new(0,0,0))
				local info = TweenInfo.new(.5)
				local tween = TweenService:Create(controller,info,goal)
				tween:Play()
				tween:Destroy()
				
				tween.Completed:Connect(function()
					stand:Destroy()
				end)
				
			end
		end
    end
end)
3 Likes

Maybe server performance was affected? Or maybe the physics for your CPU to work is exhausting.

3 Likes

I don’t think it’s cause of my CPU, since my current computer is not old at all, and I cleaned it a couple days ago. Maybe it could be cause of the server performance, but I’m not sure how to fix that…

1 Like

Have you got a screenshot of one of the laggy frames with the microprofiler? It will help a lot to know what part of the frame took so long

2 Likes

How do I use the microprofiler?

1 Like

Press Ctrl/Cmd + Shift + F6 at the same time when playing the game.

2 Likes

Here’s the video with the microprofiler.

2 Likes

Also, the script is a normal script in ServerScriptService.

1 Like

See those sudden spiky parts? That’s where you’re cpu has to take so many process to make it look good. Now press Ctrl/Cmd + Shift + P while hovering you’re mouse to that spike part to see what’s going on.

2 Likes

When its lagging, hit Ctrl+P and it’ll show a full breakdown of the frame. Those ones that reach the top of the bar are the long frames causing the lag. Once you’ve paused it with Ctrl+P it’ll show all the tasks in that frame with labels so you can see what part of the frame took a while.

2 Likes

It’s because the stand is most likely laggy. I had that issue and when I removed the stand it fixed, I recommend reducing the parts. Also the script shouldn’t be a issue because redplys has code similar to yours with no lag.

1 Like

I’m not sure what all these mean, since I never have seen things like it before.

2 Likes

It didn’t reduce the lag sadly.

1 Like

Hey,

maybe it is because of this:

1.) I think you need to change you variable of tween. Change the name of goal like “goal1 =” and “tween1 =” do this for every tween action.

or it is because of:

2.) You didnt CanCollide everything off.

3.) The last thing is your tween CFrame I would not use “(controller.Part1.CFrame * CFrame.new(0,0,0))”

I hope I could help you. And I wish you the best

1 Like

Yes I think it is because every tween has the same name with different actions

How many children are inside of the stand? Essentially, how many Tweens are actually being created and played at once here?

It looks like the render step is taking most of the time, which may be a direct result of the tweens, but it could be a result of additional effects if you have any.

Have you considered using Motor6D joints or even the built-in animations system at all? I’d be curious to know if that’s a lot more performant for the same number of parts, textures and any effects.

I used Moon Animator to create the idle animation only, but I don’t think it is a part of the tweening problem.

All of the parts except for the main parts like the Torso, Arms, HumanoidRootPart, and so on are welded to those main parts with Motor6D joints.

There are no textures or effects on the stand, and all of the stand model’s parts is only meshes. Except for the main parts, of course.

I’ll try to remove some parts from the stand without making it look too bad.

You don’t necessarily need to remove parts, I was just asking how many there currently are as I notice your code creates a tween for each individual child. I did just realise that was for transparency not positioning though - apologies for misreading it initially.

Try cutting out certain things, like the transparency tweens, to see if you can identify whether the positioning is the issue or the transparency is the issue. Once you’ve narrowed down which one is causing the greater impact, you can then plan your next steps.

It seems like it is both the position, and the transparency. They deal the same exact lag impact, I tried to make the second tween use transparency and the first tween use position. Both had the exact same lag. Even in the microprofiler.

Here’s the entire script:

local rp = game:GetService("ReplicatedStorage")
local summon = rp:WaitForChild("Summon")

local TweenService = game:GetService("TweenService")

summon.OnServerEvent:Connect(function(player,isActive)
	local char = player.Character
	local hum = char.Humanoid
	local humRP = char.HumanoidRootPart

	if isActive then
		local stand = script:WaitForChild("StarPlatinumStand"):Clone()
		
		for _, part in pairs(stand:GetChildren()) do
			if part:IsA("BasePart") then
				part.Transparency = 1
			end
		end		
		
				stand.Parent = char
				stand.PrimaryPart.CFrame = humRP.CFrame

		local weld = Instance.new("ManualWeld")
		        weld.Name = "Controller"
				weld.Part0 = stand.PrimaryPart
				weld.Part1 = humRP
				weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
				weld.Parent = weld.Part0

				local animControl = stand:FindFirstChild("AnimControl")
				local standhumRP = stand:FindFirstChild("HumanoidRootPart")

				local idle = animControl:LoadAnimation(script.Idle)
				idle:Play()
		
		        for _, part in pairs(stand:GetChildren()) do
			        if part:IsA("BasePart") and part ~= standhumRP then
				        local goal1 = {}
				        goal1.Transparency = 0
				        local info = TweenInfo.new(.3)
				        local tween1 = TweenService:Create(part,info,goal1)
				        tween1:Play()
				        tween1:Destroy()
			        end
		        end
		
				local goal2 = {}
				goal2.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
				goal2.C1 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame * CFrame.new(-3,1,2))
		        local info = TweenInfo.new(.3)
				local tween2 = TweenService:Create(weld,info,goal2)
		        tween2:Play()
	        	tween2:Destroy()
			else
		        local stand = char:FindFirstChild("StarPlatinumStand")
		if stand then
			local controller = stand.PrimaryPart:FindFirstChild("Controller")
			if controller then
				
				local standhumRP = stand:FindFirstChild("HumanoidRootPart")
				
				for _, part in pairs(stand:GetChildren()) do
					if part:IsA("BasePart") and part ~= standhumRP then
						local goal3 = {}
						goal3.Transparency = 0
						local info = TweenInfo.new(.3)
						local tween3 = TweenService:Create(part,info,goal3)
						tween3:Play()
						tween3:Destroy()
					end
				end

				for _, part in pairs(stand:GetChildren()) do
					if part:IsA("BasePart") and part ~= standhumRP then
						local goal4 = {}
						goal4.Transparency = 1
						local info = TweenInfo.new(.3)
						local tween4 = TweenService:Create(part,info,goal4)
						tween4:Play()
						tween4:Destroy()
				
				tween4.Completed:Connect(function()
							stand:Destroy()
						end)
					end
				end
				
			end
		end
    end
end)