-
What do you want to achieve? Keep it simple and clear!
I am trying to create a smooth Gacha rolling system similar to popular anime games. The system should roll through approximately 50 items using aScrollingFrameandTweenServiceto animate theCanvasPosition -
What is the issue? Include screenshots / videos if possible!
Whenever theRollfunction is triggered, there is a massive lag spike (frame drop). I’ve identified that the bottleneck happens when the script prepares the 50 templates inside theScrollingFrame -
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I’m currently out of ideas on how to optimize this further
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- // CORE
local Core = require(game.Players.LocalPlayer.PlayerScripts.ClientCore.Start.Core)
local EnumTypesItems = require(game.ReplicatedStorage.GameCore.Modules.Informations.Enums.EnumTypesItems)
-- // Services
local TweenService = game:GetService("TweenService")
local Player = game:GetService("Players").LocalPlayer
-- // Elements
local PlayerGui = Core.Player.Instance:WaitForChild("PlayerGui")
local Main: ScreenGui = PlayerGui:WaitForChild("Paradox")
local GachaRolls: Frame = Main:WaitForChild("GachaRolls")
-- // Variables
local Janitor = Core.Shared.Janitor.new()
-- // Private Functions
-- // Animations
return table.freeze({
["Roll"] = function(...)
local started = tick()
Player:SetAttribute("Rolling", true)
-- Cleanup
Janitor:Cleanup()
-- Variables
local Data = (...)
if not Data then warn("no data foujnd") return end
local GachaData = Data.Gacha and Core.Shared.GachasItens:GetGacha(Data.Gacha) or Core.Shared.warriorsGacha[Data.WarriorWorld]
local isWarrior = Data.WarriorWorld ~= nil
-- Functions
local function CreateTemplate(Roll: Frame, Quantity: number, RandomGacha: boolean, Template: string)
local createdTemplates = {}
for i = 1, Quantity do
local Title: string, Data: {}
if RandomGacha then
Title, Data = Core.Shared.GachasItens:GetRandomItem(GachaData.Gacha)
else
Title = Template
Data = GachaData.Gacha[Template]
end
local TemplateClone: Frame = Roll.ScrollingFrame.Template:Clone()
TemplateClone.Name = Title
TemplateClone.Parent = Roll.ScrollingFrame
TemplateClone.Visible = true
TemplateClone.Chance.Text = Data.Chance .. "%"
TemplateClone.ItemName.Text = Title
TemplateClone:SetAttribute("Title", Title)
if not isWarrior then
TemplateClone.Icon.Image = Data.Icon
TemplateClone:FindFirstChild(Data.Rarity).Enabled = true
else
local warriorData = Core.Shared.WarriorsData[Title]
if not warriorData then warn("NO DATA FOUND??", Title) continue end
TemplateClone:FindFirstChild(warriorData.Rarity).Enabled = true
TemplateClone.Icon.Visible = false
table.insert(createdTemplates, TemplateClone)
end
if not RandomGacha then
if isWarrior then
task.spawn(function()
game.RunService.Heartbeat:Wait()
Core.Utils.GetTypeIVForItem:GetTypeIVForItem(TemplateClone.ViewportFrame, TemplateClone.Icon, Title, EnumTypesItems.WARRIOR)
end)
end
return TemplateClone
end
Janitor:Add(TemplateClone, "Destroy")
end
if isWarrior and #createdTemplates > 0 then
task.spawn(function()
for _, template in createdTemplates do
if not template.Parent then continue end
game.RunService.Heartbeat:Wait()
local title = template:GetAttribute("Title")
Core.Utils.GetTypeIVForItem:GetTypeIVForItem(template.ViewportFrame, template.Icon, title, EnumTypesItems.WARRIOR)
end
end)
end
end
-- Animate
local Tween
for _, Data in ipairs(Data.Rewards) do
local TemplateRoll: Frame = GachaRolls.Template:Clone()
TemplateRoll.Name = "Roll"
TemplateRoll.Parent = GachaRolls
TemplateRoll.Visible = true
Janitor:Add(TemplateRoll, "Destroy")
-- Creating Templates
CreateTemplate(TemplateRoll, 40, true)
local TemplateTrue: ImageButton = CreateTemplate(TemplateRoll, 1, false, Data.Reward)
CreateTemplate(TemplateRoll, 9 , true)
-- Calculate X Pos absolutepos - scrolling absolutepos / 2 + template size / 2
local X: number = TemplateTrue.AbsolutePosition.X - TemplateRoll.ScrollingFrame.AbsolutePosition.X - (TemplateRoll.ScrollingFrame.AbsoluteWindowSize.X / 2) + (TemplateTrue.AbsoluteSize.X / 2)
--print(X)
--print(TemplateTrue.AbsolutePosition.X - TemplateRoll.ScrollingFrame.AbsolutePosition.X)
-- Tweening Scrolling
Tween = TweenService:Create(TemplateRoll.ScrollingFrame, TweenInfo.new(4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), { CanvasPosition = Vector2.new(X, 0) })
Tween:Play()
Core.Managers.SoundManager:PlayUI("GachaRoll")
end
Core.UI.UIController:HUD(false, true)
Core.UI.UIController:ToggleUI(false, true)
GachaRolls.Visible = true
Tween.Completed:Wait()
local function finish()
Core.UI.UIController:ToggleUI(true, false)
if Core.UI.UIController.IsHUDToggled then
Core.UI.UIController:HUD(true, false)
else
Core.UI.UIController:HUD(false, false)
end
end
if not Player:GetAttribute("Auto_Gacha") then
GachaRolls.Visible = false
finish()
else
finish()
end
Player:SetAttribute("Rolling", false)
end,
})