#What's causing massive lag spikes on this script?

1 messages · Page 1 of 1 (latest)

onyx abyss
#

This script was fine until I added a lot more enemies. Now it's taking up a ton of performance and causing some server lag. Can anyone help?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local SoundService = game:GetService("SoundService")

local enemyFramework = require(ReplicatedStorage.EnemyFramework)
local enemyConfig = require(ReplicatedStorage.EnemyFramework.EnemyConfig)
local enemyTemplates = ReplicatedStorage:WaitForChild("EnemyTemplates")

local enemySpawns = {}

local function trackEnemy(enemy, parentFolder)
    if not enemy:GetAttribute("EnemyID") then
        local id = HttpService:GenerateGUID(false)
        enemy:SetAttribute("EnemyID", id)
        local torso = enemy:FindFirstChild("Torso")
        if torso then
            enemySpawns[id] = {
                cframe = torso.CFrame,
                name = enemy.Name,
                parent = parentFolder
            }
        end
    end
end

local function setupFolderTracking(folder)
    for _, enemy in folder:GetChildren() do
        trackEnemy(enemy, folder)
    end
    folder.ChildAdded:Connect(function(enemy)
        trackEnemy(enemy, folder)
    end)
end

setupFolderTracking(workspace.SuperMagnets)
setupFolderTracking(workspace.Enemies)

local function respawnEnemy(enemyID)
    local data = enemySpawns[enemyID]
    if not data then return end

    task.delay(10, function()
        local template = enemyTemplates:FindFirstChild(data.name)
        if not template then return end

        local clone = template:Clone()
        clone:SetAttribute("EnemyID", enemyID)
        clone:PivotTo(data.cframe or CFrame.new(0, 5, 0))
        clone.Parent = data.parent
    end)
end

local function getFirstPlayerRoot()
    for _, player in ipairs(Players:GetPlayers()) do
        local char = player.Character
        if char then
            local root = char:FindFirstChild("HumanoidRootPart")
            if root then return root end
        end
    end
end

local function processFolder(folder)
    local enemies = folder:GetChildren()
    for i = 1, #enemies do
        task.spawn(function()
            local enemy = enemies[i]
            local enemyID = enemy:GetAttribute("EnemyID")
            local torso = enemy:FindFirstChild("Torso")
            local head = enemy:FindFirstChild("Head")
            local humanoid = enemy:FindFirstChild("Humanoid")
            if not torso or not humanoid then return end

            if not head or humanoid.Health <= 0 then
                respawnEnemy(enemyID)
                enemy:Destroy()
                return
            end

            if enemy.Name == "Penguin" then return end

            local config = enemyConfig[enemy.Name]
            local impulseStrength = config and config.ImpulseStrength or 75
            local particleEmitter = torso:FindFirstChild("magnetParticle")
            local soundToPlay = math.random(1, 2) == 1
                and SoundService.SFX.supermagnetsound1
                or SoundService.SFX.supermagnetsound2

            local root = getFirstPlayerRoot()
            if not root then return end

            if folder == workspace.SuperMagnets then
                enemyFramework.addItem("Impulse", {
                    -1, impulseStrength, root, torso, soundToPlay, particleEmitter
                })
            elseif enemy.Name == "FrozenSpiky" then
                enemyFramework.addItem("Impulse", {
                    1, impulseStrength, root, torso, soundToPlay, particleEmitter
                })
            end
        end)
        task.wait(0.01)
    end
end

while true do
    processFolder(workspace.SuperMagnets)
    processFolder(workspace.Enemies)
    task.wait(1)
end
fickle osprey
#

describe the lag

#

there's more than one kind of lag.

#

also this wait stands out to me task.wait(0.01) either justify it or remove it

onyx abyss
#

woops, discord didn't notify me. Essentially, it's causing lag on the server because of high script activity, lagging the server. I've gotten it down a little but it's still really high. It started happening after adding more enemies obviously. As for the task.wait(0.01), I was just trying anything to stop the lag to be honest.

#

It reaches about 16% activity in the script performance tab.

fickle osprey
#

show more like recording and your modules

#

i otherwise dont see much that would cause too much lag. 16% activity isn't much

onyx abyss
#

Nevermind, I found out the glitch was due to majority of the enemies not having a head, meaning they were constantly respawning.

#

How do I set this to solved?