Any way I can improve this airship altitude script or is it as good as it gets?

I’ve essentially been working on a game with 3 other people and helped make an airship with an altitude script (we’ve already got a system for wasd movement through the use of bodymovers) and I was wondering if I could improve/optimize it in any way shape or form

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local seat = humanoid.SeatPart

local Einput = false
local Qinput = false

--------------------
-- VERSION 0.0.1A --
--------------------

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	
	if seat ~= nil then
	if input.UserInputType == Enum.UserInputType.Keyboard then

		if input.KeyCode == Enum.KeyCode.E then -- descend
			Einput = true
			while Einput == true do
				wait(0.1)
				if seat.Parent.MainPart.BodyPosition.Position.Y <= 44 then
					seat.Parent.MainPart.BodyPosition.Position = Vector3.new(0,44,0)
					if seat.Parent.MainPart.BodyPosition.Position.Y < 40 then
						warn("Position of airship is at a dangerously low altitude not normally achievable, please contact a mod/admin for assistance if your airship gets stuck") -- i have no clue how to make this show up client sided consoles, ugh
					end
					else
					seat.Parent.MainPart.BodyPosition.Position = seat.Parent.MainPart.BodyPosition.Position - Vector3.new(0,1,0)
						
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.Q then -- ascend
			Qinput = true
				while Qinput == true do
					wait(0.1)
					if seat.Parent.MainPart.BodyPosition.Position.Y >= 100 then
						seat.Parent.MainPart.BodyPosition.Position = Vector3.new(0,100,0)
						else
						seat.Parent.MainPart.BodyPosition.Position = seat.Parent.MainPart.BodyPosition.Position + Vector3.new(0,1,0)
							
					end
				end
			end

		end
	end

end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E then
		Einput = false
		wait(0.1)
		print("stop")
	end
	if input.KeyCode == Enum.KeyCode.Q then
		Qinput = false
		wait(0.1)
		print("stop")
	end
end)

if anyone has suggestions or comments about this feel free to state them. Any help would be much appreciated! :slight_smile:

1 Like

Checking the UserInputType in the InputBegan event is useless, as the keycode is either correct or not, regardless of which input type it is.

Additionally, constant setting of part positions is not desirable, as it can create varying visual lag for different players. I would recommend BodyForces in this case.

2 Likes

Use runService.Heartbeat:Wait() in the while loops to make it respond better.

2 Likes

The content of your if blocks look duplicated. You can simplify this by using a function like setAltitudeGoal(goal).

1 Like

I can understand where you’re coming from with userinputtype for the inputbegan event, HOWEVER; that’s an incomplete bit of the script that was (and still is) going to be used to add multi-device compatability (xbox, mobile, etc) in the future. Sorry I didn’t note that earlier.

Also as for the bodyforce use in this scenario, I have genuinely no idea how to use bodyforce as it does not keep a steady worldspace position as bodyposition does and is only capable of putting an initial force in a certain axis direction (correct me if im wrong). Could you elaborate exactly on how I could use bodyforces in this case (an example if possible)?

2 Likes

Oh, that’s my mistake. I misinterpreted some of your code. I hadn’t realized you’re already using a BodyPosition. I meant BodyMover not BodyForce. Sorry for the confusion.

After learning more about what you’re aiming for, I think the only optimization possible (that I recognize) would be to change wait(0.1) to a Heartbeat wait.

1 Like

It would be possible to use BodyForces if you use the moving object’s CFrame.LookVector and also include a BodyVelocity to act as a brake if needed, or an anchor. This method would be better in some circumstances as the airship would only be held vertically, unless moving vertically, and horizontal forces can be simulated (Impacts or crashes).