#Optimization help

1 messages · Page 1 of 1 (latest)

nimble steppe
#

I'll explain more in chat

#

Hey

#

I've got a sophisticated controller script for a crow NPC.

#

It's got some flight methods that initiate a Run Service per frame loop individually for each crow calling that method

#

I want to centralize that

oak cobalt
#

can you send how it looks rn

nimble steppe
#

ill send one example of such a method

#
    if self.currentOrbitConn then
        self.currentOrbitConn:Disconnect()
    end
    self.isScouting = true

    local t = 0
    startingAngle = startingAngle or 0
    direction = direction or 1

    local radiusX = self.ORBIT_RADIUS_X + self.orbitRadiusXOffset
    local radiusZ = self.ORBIT_RADIUS_Z + self.orbitRadiusZOffset
    local angularSpeed = self.ANGULAR_SPEED + self.angularSpeedOffset

    self.currentOrbitConn = game:GetService("RunService").Heartbeat:Connect(function(dt)
        if not self.rootPart or not self.rootPart.Parent then
            self.currentOrbitConn:Disconnect()
            return
        end

        t += dt
        local angle = startingAngle + direction * angularSpeed * t

        -- Breathing effect: radius slightly expands/contracts
        local radiusMod = 1 + math.sin(t * 0.5) * 0.05

        -- Up-down bobbing
        local wobble = self:getWobble(t, self.phaseOffset, self.VERTICAL_WOBBLE_AMPLITUDE, self.VERTICAL_WOBBLE_SPEED)
        local orbitOffset = Vector3.new(
            math.cos(angle) * radiusX * radiusMod,
            wobble.Y,
            math.sin(angle) * radiusZ * radiusMod
        )

        local targetPos = centerXZ + orbitOffset

        -- Orbit direction (tangent to the ellipse)
        local tangential = Vector3.new(
            -math.sin(angle) * radiusX,
            0,
            math.cos(angle) * radiusZ
        ).Unit * self.FLYSPEED

        self.lv.VectorVelocity = tangential
        self:faceDirection(self.rootPart.Position, Vector3.new(tangential.X, 0, tangential.Z))
    end)

    task.delay(60, function()
        self:stopOrbit()
    end)
end```
#

ask out any doubts about the code

oak cobalt
#

can u please use

nimble steppe
#

use what?

oak cobalt
#

this format

nimble steppe
#

o mb

#
function CrowLogic:startOrbiting(centerXZ: Vector3, startingAngle: number?, direction: number?)
    if self.currentOrbitConn then
        self.currentOrbitConn:Disconnect()
    end
    self.isScouting = true

    local t = 0
    startingAngle = startingAngle or 0
    direction = direction or 1

    local radiusX = self.ORBIT_RADIUS_X + self.orbitRadiusXOffset
    local radiusZ = self.ORBIT_RADIUS_Z + self.orbitRadiusZOffset
    local angularSpeed = self.ANGULAR_SPEED + self.angularSpeedOffset

    self.currentOrbitConn = game:GetService("RunService").Heartbeat:Connect(function(dt)
        if not self.rootPart or not self.rootPart.Parent then
            self.currentOrbitConn:Disconnect()
            return
        end

        t += dt
        local angle = startingAngle + direction * angularSpeed * t

        -- Breathing effect: radius slightly expands/contracts
        local radiusMod = 1 + math.sin(t * 0.5) * 0.05

        -- Up-down bobbing
        local wobble = self:getWobble(t, self.phaseOffset, self.VERTICAL_WOBBLE_AMPLITUDE, self.VERTICAL_WOBBLE_SPEED)
        local orbitOffset = Vector3.new(
            math.cos(angle) * radiusX * radiusMod,
            wobble.Y,
            math.sin(angle) * radiusZ * radiusMod
        )

        local targetPos = centerXZ + orbitOffset

        -- Orbit direction (tangent to the ellipse)
        local tangential = Vector3.new(
            -math.sin(angle) * radiusX,
            0,
            math.cos(angle) * radiusZ
        ).Unit * self.FLYSPEED

        self.lv.VectorVelocity = tangential
        self:faceDirection(self.rootPart.Position, Vector3.new(tangential.X, 0, tangential.Z))
    end)

    task.delay(60, function()
        self:stopOrbit()
    end)
end```
oak cobalt
#

@fast ice

#

do you think it'd be better to use one heartbeat

nimble steppe
#

ofc, if there's like 20> of such NPCs

oak cobalt
nimble steppe
#

ill explain

oak cobalt
#

but if it is what i would suggest to do

#

is basically

#

keep a table of all active objects

#

and im ngl

#

if im not stupid

#

you could instead of doing it as a signal

#

define the function as

#

self.currentOrbitFunc

#

and then within the main heartbeat

#

you could do

#
for i, v in activeObjects do
      task.spawn(v.currentOrbitFunc, dt)
end
#

and then

#

make Initialize function for the module

#

then if its not initialized it will start the heartbeat

nimble steppe
#

ight thanks

#

ill implement those changes

fast ice
oak cobalt
fast ice
#

so what’s the question

oak cobalt
#

Is it better to connect one heartbeat signal

#

and loop through all active objects to move them