How would I go about implementing Network Ownership for this vehicle script?

I have been trying to properly implement NetworkOwnership to this vehicle script in order to prevent players from lagging; while maintaining the integrity of the timed destroy.

I decided to post the original version of the script rather then the version which included my very embarrassing attempt of implementing it based off of the ROBLOX Dev wiki.

Any help here is GREATLY appreciated.

script.Parent.EngineOff:Play()
local speed = 0
local accel = 2--originally 2
local TimeTillDestroy = 50

local timeSincelastOccupant = 0

local FLW = script.Parent.Parent.FrontLeftWheel
local FRW = script.Parent.Parent.FrontRightWheel
local BLW = script.Parent.Parent.BackLeftWheel
local BRW = script.Parent.Parent.BackRightWheel

while wait(.02) do
	if script.Parent.Occupant then
		if speed > 10 and script.Parent.Occupant.Parent.HumanoidRootPart.Velocity.magnitude < speed then
			
			speed = script.Parent.Occupant.Parent.HumanoidRootPart.Velocity.magnitude
		end
		timeSincelastOccupant = 0
	else
		if timeSincelastOccupant > TimeTillDestroy then
			script.Parent.Parent:Destroy()
		end
		speed = 0
		timeSincelastOccupant = timeSincelastOccupant +.04
	end
	if script.Parent.Throttle == 1 then
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed < 0 then
			speed = speed+accel
		elseif speed < 35 then--originally 35
			speed = speed+accel/5
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.SmoothPlastic
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = false
	elseif script.Parent.Throttle == -1 then
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed >= 0 then
			speed = speed-accel
		elseif speed > -15 then
			speed = speed-accel/5
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = true
	else
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed > 0 then
			speed = speed-accel
		elseif speed <= 1 and speed >= -1 then
			speed = 0
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = true
	end
	
	script.Parent.EngineOff.PlaybackSpeed = 1+math.abs(speed)/20
	
	
	script.Parent.Parent.FrontRightSteer.HingeConstraint.TargetAngle = script.Parent.Steer*17	
	script.Parent.Parent.FrontLeftSteer.HingeConstraint.TargetAngle = script.Parent.Steer*17
	
	if game.Lighting.ClockTime > 6.5 and game.Lighting.ClockTime < 17.5 then
		script.Parent.Parent.CarModel.HeadLights.Material = Enum.Material.SmoothPlastic
		script.Parent.Parent.CarModel.HeadLights.HeadLight.Enabled = false
	else
		script.Parent.Parent.CarModel.HeadLights.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.HeadLights.HeadLight.Enabled = true
	end
	
end

First of all you can change while wait(.02) do to while wait() do

And about the lag you are talking about, do you mean input lag? if so i recommend putting the controls in a local script. It will remove the input lag

https://www.roblox.com/games/6425690762/Springbrook-RP-BETA#

There’s reference to what kind of lag I’m speaking about. I’m not sure exactly what kind of lag, but I figured network ownership should solve it?

Seeing if you go out of the vehicle, and back in - it sometimes fixes it.

This was how it was set up (wouldn’t drive)

script.Parent.EngineOff:Play()
local speed = 0
local accel = 2--originally 2
local TimeTillDestroy = 50

local timeSincelastOccupant = 0

local FLW = script.Parent.Parent.FrontLeftWheel
local FRW = script.Parent.Parent.FrontRightWheel
local BLW = script.Parent.Parent.BackLeftWheel
local BRW = script.Parent.Parent.BackRightWheel

while wait(.02) do
	local VehicleSeat = script.Parent

	VehicleSeat.Changed:Connect(function(prop)
		if prop == "Occupant" then
			local humanoid = VehicleSeat.Occupant
			if humanoid then
				local player = game:GetService("Players"):GetPlayerFromCharacter(humanoid.Parent)
				if player then
					VehicleSeat:SetNetworkOwner(player)
					if speed > 10 and script.Parent.Occupant.Parent.HumanoidRootPart.Velocity.magnitude < speed then

						speed = script.Parent.Occupant.Parent.HumanoidRootPart.Velocity.magnitude
					end
					timeSincelastOccupant = 0
			else
					VehicleSeat:SetNetworkOwnershipAuto()
				end
				if timeSincelastOccupant > TimeTillDestroy then
					script.Parent.Parent:Destroy()
				end
			end
			end
			end)
	
		speed = 0
	timeSincelastOccupant = timeSincelastOccupant +.04
	
	if script.Parent.Throttle == 1 then
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed < 0 then
			speed = speed+accel
		elseif speed < 35 then--originally 35
			speed = speed+accel/5
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.SmoothPlastic
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = false
	elseif script.Parent.Throttle == -1 then
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed >= 0 then
			speed = speed-accel
		elseif speed > -15 then
			speed = speed-accel/5
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = true
	else
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed > 0 then
			speed = speed-accel
		elseif speed <= 1 and speed >= -1 then
			speed = 0
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = true
	end
	
	script.Parent.EngineOff.PlaybackSpeed = 1+math.abs(speed)/20
	
	
	script.Parent.Parent.FrontRightSteer.HingeConstraint.TargetAngle = script.Parent.Steer*17	
	script.Parent.Parent.FrontLeftSteer.HingeConstraint.TargetAngle = script.Parent.Steer*17
	
	if game.Lighting.ClockTime > 6.5 and game.Lighting.ClockTime < 17.5 then
		script.Parent.Parent.CarModel.HeadLights.Material = Enum.Material.SmoothPlastic
		script.Parent.Parent.CarModel.HeadLights.HeadLight.Enabled = false
	else
		script.Parent.Parent.CarModel.HeadLights.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.HeadLights.HeadLight.Enabled = true
	end
	end

Just noticed my mistake:

script.Parent.EngineOff:Play()
local speed = 0
local accel = 2--originally 2
local TimeTillDestroy = 50

local timeSincelastOccupant = 0

local FLW = script.Parent.Parent.FrontLeftWheel
local FRW = script.Parent.Parent.FrontRightWheel
local BLW = script.Parent.Parent.BackLeftWheel
local BRW = script.Parent.Parent.BackRightWheel

while wait(.02) do
	local VehicleSeat = script.Parent

	VehicleSeat.Changed:Connect(function(prop)
		if prop == "Occupant" then
			local humanoid = VehicleSeat.Occupant
			if humanoid then
				local player = game:GetService("Players"):GetPlayerFromCharacter(humanoid.Parent)
				if player then
					VehicleSeat:SetNetworkOwner(player)
					if speed > 10 and script.Parent.Occupant.Parent.HumanoidRootPart.Velocity.magnitude < speed then

						speed = script.Parent.Occupant.Parent.HumanoidRootPart.Velocity.magnitude
					end
					timeSincelastOccupant = 0
			else
					VehicleSeat:SetNetworkOwnershipAuto()
					speed = 0
					timeSincelastOccupant = timeSincelastOccupant +.04
				end
				if timeSincelastOccupant > TimeTillDestroy then
					script.Parent.Parent:Destroy()
				end
			end
			end
			end)
	
	if script.Parent.Throttle == 1 then
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed < 0 then
			speed = speed+accel
		elseif speed < 35 then--originally 35
			speed = speed+accel/5
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.SmoothPlastic
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = false
	elseif script.Parent.Throttle == -1 then
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed >= 0 then
			speed = speed-accel
		elseif speed > -15 then
			speed = speed-accel/5
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = true
	else
		FLW.HingeConstraint.AngularVelocity = speed
		FRW.HingeConstraint.AngularVelocity = speed
		BLW.HingeConstraint.AngularVelocity = -speed
		BRW.HingeConstraint.AngularVelocity = -speed
		if speed > 0 then
			speed = speed-accel
		elseif speed <= 1 and speed >= -1 then
			speed = 0
		end
		script.Parent.Parent.CarModel.BackLights1.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.BackLights1.BackLight.Enabled = true
	end
	
	script.Parent.EngineOff.PlaybackSpeed = 1+math.abs(speed)/20
	
	
	script.Parent.Parent.FrontRightSteer.HingeConstraint.TargetAngle = script.Parent.Steer*17	
	script.Parent.Parent.FrontLeftSteer.HingeConstraint.TargetAngle = script.Parent.Steer*17
	
	if game.Lighting.ClockTime > 6.5 and game.Lighting.ClockTime < 17.5 then
		script.Parent.Parent.CarModel.HeadLights.Material = Enum.Material.SmoothPlastic
		script.Parent.Parent.CarModel.HeadLights.HeadLight.Enabled = false
	else
		script.Parent.Parent.CarModel.HeadLights.Material = Enum.Material.Neon
		script.Parent.Parent.CarModel.HeadLights.HeadLight.Enabled = true
	end
	end

Sleitnick has a very good tutorial playlist on how to make vehicles that you can find here. I recommend looking at the server script and client script episodes. The benefit of what he uses is the server script for detecting change in the driver seat occupants and setting network ownership, then cloning the client script to the player’s backpack. From there, the client script has full control over the car. You might need to change the client script from what he wrote to control the car for however you have it set up, though.

Important note: in the videos, there weren’t any other seats so if you want to add those too, you’re going to need to detect when somebody sits in them and set their character’s parts to massless, just like the tutorial has you do for the driver seat. This is important to stop the car from glitching when a passenger sits in the seat. Don’t forget turn of massless for their character though!