How to reduce/prevent lag when adding multiple instances at once

I’m using the follow code to make a 10x summon system:

Summon_Event10x.OnServerInvoke = function(Player)

    local UnitsInventory = Player:WaitForChild("UnitsInventory")
    local Gems = Player:WaitForChild("Gems")

    if Gems.Value < 400 then return end
    Gems.Value = Gems.Value - 400
    
    local Units = {}

    local Banner = CurrentBanner
    
    for i = 1,10,1 do
        local Chance = math.random(1,100)
        local Counter = 0
        for _,Unit in pairs(Banner) do
            Counter = Counter + tonumber(Unit.Percentage)
            if Chance <= Counter then
                local UnitInstance = Instance.new("StringValue")
                UnitInstance:SetAttribute("UUID",HttpService:GenerateGUID())
                UnitInstance.Name = Unit
                UnitInstance.Value = Unit
                UnitInstance.Parent = UnitsInventory
                table.insert(Units,Unit.Name)
                break
            end
        end
    end
    
    return Units
    
end

However when this RemoteFunction is Invoked there is a lot of lagged/frame rate drops to 0. Is there anyway to make this better/reduce or if possible prevent lag/FPS drop?

1 Like

An easy solution is to add yielding to the script:

		break
	end
	task.wait() --waits for a single frame
end

I’ve also heard that when dealing with large amounts of instances, cloning is better than instancing, so you can also do that:

--outside the loop:
local sample = Instance.new("StringValue")
--inside the loop:
local UnitInstance = sample:Clone()

Thank you I’ll try this out, and see if it works

This is the new code, and it’s better but the FPS still drops a lot and there is a lot of lag still

Summon_Event10x.OnServerInvoke = function(Player)

    local UnitsInventory = Player:WaitForChild("UnitsInventory")
    local Gems = Player:WaitForChild("Gems")

    if Gems.Value < 400 then return end
    Gems.Value = Gems.Value - 400
    
    local Units = {}

    local Banner = CurrentBanner
    
    local UnitTemplate = 
    
    for i = 1,10,1 do
        local Chance = math.random(1,100)
        local Counter = 0
        for _,Unit in pairs(Banner) do
            Counter = Counter + tonumber(Unit.Percentage)
            if Chance <= Counter then
                local UnitInstance = UnitTemplate:Clone()
                UnitInstance:SetAttribute("UUID",HttpService:GenerateGUID())
                UnitInstance.Name = Unit.Name
                UnitInstance.Value = Unit.Name
                UnitInstance.Parent = UnitsInventory
                table.insert(Units,Unit.Name)
                task.wait()
                break
            end
        end
        task.wait()
    end
    
    return Units
    
end

Alright, I figured out the problem. Basically it had little to do with the server script but all to do with the way I was handling the Units being added to the inventory. [I fixed it]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.