I've made a primitive fly script, how can I make the character go in multiple directions? (As well as make the code a lot better.)

I have a pretty barebones fly script that looks like this.

The main problem with it is that you can’t hold, say, ‘W’ and ‘D’ to go diagonally forward. If that makes sense.

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
character:WaitForChild("Humanoid")
character:WaitForChild("HumanoidRootPart")

local flying = false
local lastTime = os.clock()

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	local flyIdleAnimation = Instance.new("Animation")
	flyIdleAnimation.AnimationId = "rbxassetid://5692953945"
	local flyIdle = character.Humanoid:LoadAnimation(flyIdleAnimation)
	flyIdle.Name = "FlyIdle"
	
	if input.KeyCode == Enum.KeyCode.Space then
		if os.clock() - lastTime < 0.4 then
			flying = not flying
			if flying then
				flyIdle:Play(0.2)
				local flyBV = Instance.new("BodyVelocity")
				flyBV.Name = "FlyBV"
				flyBV.MaxForce = Vector3.new(1e6,1e6,1e6)
				flyBV.Velocity = Vector3.new(0,0,0)
				flyBV.Parent = character.HumanoidRootPart

				local force = 35
				while flying do
					local keys = UserInputService:GetKeysPressed()
					for _, key in pairs(keys) do
						if key.KeyCode == Enum.KeyCode.W then
							flyBV.Velocity = camera.CFrame.LookVector * force
						elseif key.KeyCode == Enum.KeyCode.A then
							flyBV.Velocity = (character.HumanoidRootPart.CFrame.RightVector * -1) * force
						elseif key.KeyCode == Enum.KeyCode.S then
							flyBV.Velocity = (character.HumanoidRootPart.CFrame.LookVector * -1) * force
						elseif key.KeyCode == Enum.KeyCode.D then
							flyBV.Velocity = (character.HumanoidRootPart.CFrame.RightVector) * force
						elseif key.KeyCode == Enum.KeyCode.E then
							flyBV.Velocity = (character.HumanoidRootPart.CFrame.UpVector) * force
						elseif key.KeyCode == Enum.KeyCode.Q then
							flyBV.Velocity = (character.HumanoidRootPart.CFrame.UpVector * -1) * force
						else
							flyBV.Velocity = Vector3.new(0,0,0)
						end
					end
					RunService.Heartbeat:Wait()
				end
			else
				if character.HumanoidRootPart:FindFirstChild("FlyBV") then
					character.HumanoidRootPart.FlyBV:Destroy()
				end
				for _, track in pairs(character.Humanoid:GetPlayingAnimationTracks()) do
					if track.Name == "FlyIdle" then
						track:Stop(0.2)
						break
					end
				end
			end
		end
		lastTime = os.clock()
	end
	
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed or not flying then return end

	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.Q then
		if character.HumanoidRootPart:FindFirstChild("FlyBV") then
			character.HumanoidRootPart.FlyBV.Velocity = Vector3.new(0,0,0)
		end
	end
end)
3 Likes