#Optimization help
1 messages · Page 1 of 1 (latest)
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
can you send how it looks rn
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
can u please use
use what?
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```
ofc, if there's like 20> of such NPCs
well thats the thing im not sure
ill explain
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
where it will do this
i am NOT reading ts on phone
so what’s the question