#Scripting optimisation

1 messages · Page 1 of 1 (latest)

pure oriole
#

Hey, i am trying to make a summon system which works fine but i started to a massive performance issue

When i summon 1 unit i have a small freeze and ping stays the same (which is still bad) BUT when i summon 10 units i lag for multiple seconds and reach between 500 to 900ms (i have 20ms as default ping)

I tried to look trough my functions and test the latency but its not the problem, the performance issue is related to something else

local start = os.clock() -- at the start of my function
print("Function duration:", os.clock() - start, "seconds") -- at the end

The problem is clearly related to these 2 functions but i have no idea what i can do to optimise/avoid lag (its my first roblox game, even tho i have a bit of experience making gmod servers and discord ccs)
This is how far i could get using GPT to optimise the function but i still dont know what makes it lag so much

(dont mind the bad returns of informations, i will do all the feedback part later)

#

These are the 2 functions :

local function RewardUnitsToPlayer(player, unitResults)
    local rewardedUids = {}

    for _, result in ipairs(unitResults) do
        local unitId = result.UnitID
        local uid = UnitManager.GiveUnitToPlayer(player, unitId)

        if uid then
            table.insert(rewardedUids, {
                UID = uid,
                UnitID = unitId,
                Rarity = result.Rarity
            })
        else
            warn("Impossible de donner l’unité au joueur :", unitId)
        end
    end

    return rewardedUids
end
#
function UnitManager.GiveUnitToPlayer(player, unitId, extraData)
    local profile = DataManager.Profiles[player]
    if not profile or not unitId then return nil end

    local unitTemplate = UnitsWiki[unitId]
    if not unitTemplate then return nil end

    local uid = DataManager.GenerateUID()
    local unitData = {
        UnitId = unitId,
        XP = 0,
        Level = 1,
        EquippedItems = {
            Ring = "None",
            Earring = "None",
            Weapon = "None",
            Necklace = "None",
            Core = "None",
        },
        UnlockedDate = os.time(),
        IsEvolved = false,
        IsFavorite = false,
        LimitBreak = false,
        CoreBonus = {},
        Trait = "None",
    }

    if unitTemplate.EvolveRequirements then
        unitData.Progression = {
            Conditions = {},
            Items = {},
        }
        for i, cond in ipairs(unitTemplate.EvolveRequirements.Conditions or {}) do
            local progress = {
                Type = cond.Type,
                Completed = false,
                DependsOn = cond.DependsOn,
                Current = (cond.Type == "DungeonCompletion" or cond.Type == "AllyDeaths") and 0 or nil
            }
            unitData.Progression.Conditions[i] = progress
        end
        for itemId, req in pairs(unitTemplate.EvolveRequirements.Items or {}) do
            unitData.Progression.Items[itemId] = { AmountRequired = req.Amount }
        end
    end

    for key, value in pairs(extraData or {}) do
        if unitData[key] ~= nil then
            unitData[key] = value
        end
    end

    profile.Data.Units = profile.Data.Units or {}
    profile.Data.Units[uid] = unitData
    UpdateUnits:FireClient(player, profile.Data.Units)
    return uid
end
#

And this is the structure of unitResults used in RewardUnitsToPlayer
Here its 1 to 10 but if i do 1 summon there is only 1, still, it makes me lag

{
   [1] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [2] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [3] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [4] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [5] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [6] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [7] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [8] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [9] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   },
   [10] =  ▼  {
      ["UnitID"] = "UNIT_ID"
   }
bleak otter
#

is profile.Data.Units containing much data?

UpdateUnits:FireClient(player, profile.Data.Units)
pure oriole
bleak otter
#

i mean this could cause the lag spike

pure oriole
#

yeah its clearly the reason, ill try to work around this issue

bleak otter
#

fr this code is already better then 95% of help requests we get sent in hereevilcat