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’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.
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
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
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)
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
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?