Game lagging when tween plays

I’ll try to do that, even though I’m still new to for loops and tables.

Okay, actually, I think it might be because of how many meshes there are. maybe, if I change the meshes to parts, or delete some meshes, I might get a bit less lag.

1 Like

Yes. but I would keep the rest of summon script, and call LocalScript with RemoteEvent to tell LocalScript when to start the tween.

Thanks, that actually is a good idea.

And still it will lag since there would be more than one player in server I guess

By the way, I think you don’t have to call the function Destroy since when the Instance is parented to nil and there are no more reference to it, it will eventually be garbage collected. As far as I know…

Actually, I don’t know what to type in LocalScript.

Should I add the tweens into the LocalScript and then play them when they are meant to be played by using a RemoteEvent, or should I do something else?

Edit: Nevermind, I found out what to do.

Also, where should I put the LocalScript?

Yes, you are far correct.

I prefer placing it in StarterPlayerScripts

For some reason, this happens when I summon it.

This is what it looks like from the explorer:
explorerstandsummonscript

SummonClient’s Code:

local rp = game:GetService("ReplicatedStorage")
local summon = rp:WaitForChild("Summon")
local tween1event = rp:WaitForChild("Tween1")
local tween2event = rp:WaitForChild("Tween2")
local tween3event = rp:WaitForChild("Tween3")
local tween4event = rp:WaitForChild("Tween4")

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
				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
			end
		end

				local standhumRP = stand:FindFirstChild("HumanoidRootPart")
				
		tween1event.OnServerEvent:Connect(function()
			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
		end)
					
					local stand = char:FindFirstChild("StarPlatinumStand")
					if stand then
						local controller = stand.PrimaryPart:FindFirstChild("Controller")
						if controller then

							local standhumRP = stand:FindFirstChild("HumanoidRootPart")
							tween3event:FireServer()
							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
							tween4event:FireServer()
							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)

Summon’s Code:

local rp = game:GetService("ReplicatedStorage")
local summon = rp:WaitForChild("Summon")
local tween1event = rp:WaitForChild("Tween1")
local tween2event = rp:WaitForChild("Tween2")
local tween3event = rp:WaitForChild("Tween3")
local tween4event = rp:WaitForChild("Tween4")

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()
		
		tween1event:FireServer()
		        for _, part in pairs(stand:GetChildren()) do
			        if part:IsA("BasePart") and part ~= standhumRP then
				        local goal1 = {}
				        goal1.Transparency = 0
				        local info = TweenInfo.new(.01)
				        local tween1 = TweenService:Create(part,info,goal1)
				        tween1:Play()
				        tween1:Destroy()
			        end
		        end
		tween2event:FireServer()
				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")
		tween3event:FireServer()
				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
		tween4event:FireServer()
				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)

Did I do anything wrong?

I have few questions;

  1. Summon being duplicated twice or it being located on the player, which is a problem?
  2. Is the lag issue resolved?

I’m not sure, I haven’t looked into it too much yet.

Yes, it is resolved, according to the microprofiler.

Could you specify the problem? Since I do not know what you’ve intended for, I can’t find any problem just with video.

The problem is basically the stand not moving to the place that it’s supposed to move, and it won’t get destroyed when the final tween is over.

Try changing :FireServer() in Summon Script to :FireAllClients(), and .OnServerEvent in Summon Local Script to .OnClientEvent.

Summon Script is server script and Summon Local Script is local script, so when Summon Script is calling Summon Local Script, you should fire to clients, not server.

Hey there,
Since you’re tweening so many parts’ transparency at once to 0, this may be what’s causing your lag. If this is on a ServerScript, this is actually very likely to be the case of the matter. Try removing the transparency tweens and see what happens.
You may also be experiencing issues with so many parts being rendered instantly at once, since it’s just popping in when you summon it. This may also be an issue with :GetChildren(), since you’re trying to get so many parts in your rig, assuming that you have a lot for the server to deal with, and trust me, using :GetChildren() on a lot of parts will LAG. If you can, try to mitigate this issue.

1 Like

Removing the transparency sure did a change, but the summon is still a bit laggy, I’m not sure how to mitigate the :GetChildren() issue, so what I’ll try to do to reduce the lag from the summon movement, is:

Make an animation for the summon, make it play when you summon, then instantly move the stand to the destinated position when the idle animation is played.

It will be hard, but I’ll try my best.

1 Like

It is lagging most likely because you’re cloning a lot of parts. I had this problem and I tested it with a normal dummy and it didn’t lag but it did lag with a stand that has a lot of parts. So maybe try cloning the script at first BUT when you unsummon don’t delete/destroy it just keep it invisible and then use the same stand when you summon again.

I know this is an extremely late reply, but I actually found a solution to this problem. I was experiencing a similar issue, and I found that if you parent the stand model to workspace instead of the character, it completely eliminates the lag with tweening the transparencies of all the parts and tweening the weld’s offset.

1 Like