Managing - Loops & Performance?

I’m working on a Starship system that relies on loops during use;
To go into more detail - When the ship is occupied and being piloted a constant loop will be active in order to allow movement.

while Pilot do wait()
-- Ship moves according to the Throttle, SetSpeed etc.
end

Another loop will also be running - Registering Planets that are nearby as:

while Pilot do wait()
-- if (Ship.Position - Planet.Position).Magnitude < 200 then inrange
end

Is there any way to run my Loops smoothly - Allowing for these checks but ensuring efficient performance?

There’s really not much to be said about it really. Use events whenever possible and use as few loops as possible. Only have loops run as often as necessary.

As for the example you gave, they both have the same loop requirements, so you can simply combine them.

1 Like

Thanks,

My ship uses VehicleSeat - So in the past I had tried to use .Changed, however, this wasn’t very effective the turning the ship etc as each (Throttle or Steer) would counteract the other.

If you’re doing space simulation, I would recommend using things like BodyForce, BodyVelocity, and/or BodyGyro to move the ship. You can use the changed event for change of input (speed and direction). As for being in range of a planet, you can use either a touch event (using a hitbox that surrounds the planet) or a Region3 of the given range around said planet.

1 Like

yeah make the loops into one because they both run under the same circumstances and it would improve perfomance

and have these run under a function of when the pilot is true like

local function PilotsTrue()
  while wait(1) do 
    ---do all your stuff 
  end 
end

something like that because i dont have studio open

also if u want to know why they should share the same loop and why it would effect perfomnance feel free to ask

2 Likes

As of now, it moves along a restricted Y Axis - Basic movement forward, backwards and turning.
Using PropertyChanged, how would I allow for the movement left/right without using Loops?

It tilts as it turns so I use BodyGyro for that to .CFrame and Rotate the base.

1 Like

Left and Right are both orientational movements. Unless hovering, most air/space vehicles only “move” forward and backwards. Although the Y axis can be used as well if for something like a helicopter. But for what you’re doing, you’ll want to use BodyGyro for everything involving direction. Forward and backward movement can use the other forces I mentioned.

As for PropertyChanged, it depends on how your control / input system works.

my genetic algorithm runs like a million times a second but like idk how to optimize that. it is a fundamental for the fitness function to be ran alot but luckily computers like math.

In terms of this - How it was said that I can use the While loop for both functions - When it comes to checking the .Magnitude of whether or not the Ship is within range, would it be better for me to use the .Changed event for this loop, meaning that the While loop can solely manage piloting?

You’re most likely asking for loop performances because you don’t want to put a tax on a server, so you should try handling vehicle operations on the client, even planet checking if you think if it’s safe for your players to handle that calculation.

The most important rule of doing loops imo is necessity: is it necessary to scan the workspace 1/30th-1/60th a second to do xyz, is there any possible ways to cache the way you’re doing things including adding variables, are there any connections/events you can take advantage of, etc.

1 Like