Tips on creating server/client hybrid vehicle system

Recently i made a controller for a semi realistic helicopter and while its been working well so far its got one problem, its all client side. if a pilot leaves the heli it becomes a brick with no behavior and falls straight down.

I want to add some server side behavior to control how the heli behaves while pilotless but don’t know how to exactly structure it so that the heli uses a hybrid of both client and server side scripts while also being able to use the player input when a driver is present.

I tried searching for tutorials online for vehicle/car controllers to find a way, but almost all either use a 3rd party chassis or use VehicleSeats, which i don’t want to use since my helicopter requires more controls than just steer and accelerate. i’m not asking for someone to write the system for me, just need tips on how to strcture my code so that it works flawlessly on client/server,.

1 Like

I think the best solution would be to put all the helicopter-controlling code (i.e. the stuff that adjusts the velocitys and what-have-you) into a ModuleScript, have a Script with RunContext of Client (or a LocalScript) that uses the module when the player is controlling the helicopter, but then have a regular Script with RunContext Server (the default) that only affects the helicopter when there isn’t a player in it, using the same helicopter controls module.

So, something like:

-- Server-script

RunService.Heartbeat:Connect(function(DeltaTime)
    if Helicopter.Pilot.Value then
        return -- the "fake pilot" should not be active when there's a real pilot
    end
    -- do whatever, like force the heli into a free-fall by (as an example)
    -- aiming it downwards
    HelicopterModule.SetAim(Helicopter, -Vector3.yAxis)
end)

-- Local-script
-- Just here to make the example feel more complete

RunService.Heartbeat:Connect(function(DeltaTime)
    if not PilotingHeli then
        return
    end
    -- helicopter controls stuff
    HelicopterModule.SetAim(PilotingHeli, Camera.lookVector)
end)

Alternatively, depending on if the game is mainly intended to be a single-player experience, or if it really just doesn’t matter, you can force the helicopter to always have it’s physics controlled by the last player that controlled it with :SetNetworkOwner on it’s root part.


This is all assuming that the issue is that, when the player leaves the helicopter, the player loses “Network Ownership” of it, and that is what you are trying to solve the effects of.