Screen freezing when Tweening hundreds of parts

Hello! i did Game Over screen for my game it’s doing invisible every map parts and making visible black box for “Void” feeling. But for some reason game freezing when tweening parts.

Video:

Code:

local AppearGameOverPart = TweenService:Create(GameOverPart, TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Transparency = 0}):Play()

			for i, v in pairs(workspace.MAP:GetDescendants()) do
				if v:IsA("Part") or v:IsA("Texture") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
					local DisappearMap = TweenService:Create(v, TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Transparency = 1}):Play()
				end
			end

I’ll be so glad, if you guys help me.

1 Like

Are you tweening on the client or server?

I’m tweening on client, not server.

I’ve tried to put some variables on the outside and shorten the if check. I’ve also replaced pairs with generalized iteration because it’s faster.

Code:

local presetTweenInfo = TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local tweenGoal = {Transparency = 1}

for i, v in workspace.MAP:GetDescendants() do
	if v:IsA("BasePart") or v:IsA("Texture") then
		TweenService:Create(v, presetTweenInfo, tweenGoal):Play()
	end
end

If this doesn’t work, I’ll try optimizing it more.

Sorry, but it’s still same. Didn’t changed anything. :frowning:

1 Like

I’ve added a wait, could you try this?

Code:

local presetTweenInfo = TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local tweenGoal = {Transparency = 1}

for i, v in workspace.MAP:GetDescendants() do
	if v:IsA("BasePart") or v:IsA("Texture") then
		TweenService:Create(v, presetTweenInfo, tweenGoal):Play()
		task.wait()
	end
end

It’s not freezing now but it’s waiting to all parts getting invisible.

you could set some of thems transparency to 1 instantly, maybe only tween big ones with checking size or magnitude of it?

I’ll make it wait every 10 instances so it’s less noticeable. This lag is pretty inevitable though, especially since there are hundreds of parts.

Code:

local presetTweenInfo = TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local tweenGoal = {Transparency = 1}

for i, v in workspace.MAP:GetDescendants() do
	if v:IsA("BasePart") or v:IsA("Texture") then
		if i % 10 == 0 then
			task.wait()
		end
		TweenService:Create(v, presetTweenInfo, tweenGoal):Play()
	end
end
1 Like

Still waiting too much, i’ll try to do with magnitude as @sinanxd2 said

I used this magnitude but still freezing when tweening MeshParts and Unions.

how big is workspace.MAP:GetDescendants() table? If its big you can create a folder in it that only has buildings, i don’t see how tweening transparency would lag if its not checking thousands of parts

Could you try this? I made it run in a new thread and for it to wait every 50 instances.

Code:

local presetTweenInfo = TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local tweenGoal = {Transparency = 1}

task.spawn(function()
	for i, v in workspace.MAP:GetDescendants() do
		if v:IsA("BasePart") or v:IsA("Texture") then
			if i % 50 == 0 then
				task.wait()
			end
			TweenService:Create(v, presetTweenInfo, tweenGoal):Play()
		end
	end
end)


This is the MAP Folder, i’m tweening all because parts shouldnt seen at dark box.

Thanks, this is not doing freeze but game over screen should be like this:


it will be tweened in same time.

Tip: Always check your frames profiler when your game freezes.

You’re probably using too much processing per frame. An easy way to fix that is to split it up a little bit.

I bet the bulk of the processing is just creating so many instances (tweens are instances) in one frame.

Try this code instead:


local tweens = {}
local maxTweenCreationsPerFrame = 100

-- Create the tweens across multiple frames
for i, v in pairs(workspace.MAP:GetDescendants()) do
	count = 1
	if v:IsA("Part") or v:IsA("Texture") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
		local DisappearMap = TweenService:Create(v, TweenInfo.new(WaitTime2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Transparency = 1})
		table.insert(tweens, DisappearMap)

		count += 1
		if count >= 100 then
			count = 1
			task.wait()
		end
	end
end

-- Start the tweens all at once
for _, tween in ipairs(tweens):
	tween:Play()
end

-- Clean up the tweens
for index, tween in ipairs(tweens):
	tween:Destroy()
	tweens[index] = nil
end
tweens = nil

It’s still freezing, didnt changed.

1 Like

Huh. What does your profiler say?

This seems like a LOT of extra work to get the effect you want.
Do you think you might be able to get the same effect, if you use a GUI screen to fade to black, but have over it, a viewport frame with the character that does not fade?

3 Likes

I don’t know is this can be done with viewport frame.