How to reference Left Control button detection with Mouse.Keydown event

I’m attempting to create a sci-fi game with a friend of mine with a flight system that has essentially a flight seat provided by MaxR3d with some modifications of our own in order to increase the functionality (warp speed, velocity changes etc etc) and we are currently trying to reference the left control button but apparently I’ve had trouble finding out how to do so without substantially modifying any of the functions within the main script as I do not want to have to rework and possibly break it. Here’s the function and the main keys just for reference (0, or shift rather increases the ship velocity and z at the moment decreases it):

mouse.KeyDown:connect(function(key)
if key == "0" then
		veri = 0.006
	end
if key == "z" then
		veri = -0.006
    end

again I do not wish to go through the headache of having to rework any of the functions any more than I need to mainly due to the circumstance that I have not in any way had prior experience in handling KeyDown functions or even UserInputService for that matter. If it’s unavoidable that I’ll need to implement userinputservice instead, please let me know but otherwise I would like to know if there’s a string identification for left control.

TL;DR: Not experienced with KeyDown/UserInputService, played around/modified MaxR3d’s enhanced flight seat and need a string identifier for left control instead of using z if possible or other solution if necessary

Don’t know if you need the rest of the code but you can find the model here and check the script for yourself: (14) Enhanced Flight Seat - Roblox

I know you have no experience with UserInputService but it’s really simple:

game:GetService("UserInputService").InputBegan:Connect(function(key, processed)
   if processed then return end -- this checks if the player is typing in a textbox or pressing a button

   if key.KeyCode == Enum.KeyCode.LeftControl then -- checks if the left mouse button was pressed
     -- do stuff
   end
end

You would have to use UIS or ContextActionService at some point anyway since Mouse and Mouse.KeyDown are both deprecated (doesn’t mean removed).


I changed and implemented userinput service as of today, however; now the script won’t even prompt changes in the body velocity velocity values when the player presses down on the correct keys. I’ll press w to go forwards or s to go backwards but nothing happens. Can you explain this?

Entirety of code so far:

local player = game.Players.LocalPlayer
local gui = script:WaitForChild'SHIP_GUI'
local Fightermodel = script.Parent.Parent:WaitForChild("Fighter")
local Thrust1 = Fightermodel["Pilot Seat"].SpaceshipThrust
local warpzone = script.Warpzone
--local wireLib = require(workspace.WireLib)
local ctrlBind = {}
ctrlBind.Top = "r"
ctrlBind.Bottom = "."
ctrlBind.Front = "w" -- non-functional/needs fix
ctrlBind.Back = "s" -- non-functional/needs fix
ctrlBind.Left = "."
ctrlBind.Right = "."
ctrlBind.Controls = "c"
fastKey = "h"
signal = 0
ctrlBool = {}
ve = 0
oSpeed = 0
vec = 0
mo = 0
firing = false
fastOn = false
function getThrust(seat)
	return 10
end
function hasWormholeTech(seat)
	return true
end
function hasFTLTech(seat)
	return true
end
function accountThrusters(thrusters)
	return .6
end
function getMass(seat)
	return 10
end
for i, m in pairs(ctrlBind) do
	ctrlBool[i] = false
end
function turnDirectionConform(vector,vector2)
	local e = vector2*CFrame.new(-1,0,0)
	local b = vector2*CFrame.new(1,0,0)
	local z = vector2 + vector
	local left = e-z.p
	local right = b-z.p
	if left.p.Magnitude > right.p.Magnitude then return 1 end
	return -1
end
function updateVectors(cam, mouse,seat)
	local optimalCF = seat.CFrame.p
	local dir = CFrame.new(Vector3.new(), (mouse.UnitRay.Direction) + seat.CFrame.lookVector)
	local cf = dir
	local lv = dir.lookVector
	local cframe = dir
	local tab = {}
	tab.Vector = Vector3.new()
	for i, m in pairs(ctrlBool) do
		if i ~= "Fast" and i ~= "Controls" then
		if Enum.NormalId[i] and m then
			tab.Vector = tab.Vector + Vector3.FromNormalId(Enum.NormalId[i])
		end
		end
	end
	local finalVector = CFrame.new(tab.Vector.Unit.Magnitude < 5 and tab.Vector.Unit or Vector3.new())
	local turning = dir.lookVector:Dot(seat.BodyGyro.cframe.lookVector)
	ve = ve + vec
	if vec == 0 then
		ve = 0
	end
	local turn = ((mouse.UnitRay.Direction) - seat.CFrame.lookVector).Magnitude
	local ver = ve * 6
	if gyro then
		local g = (dir *CFrame.fromEulerAnglesXYZ(0, 0, math.rad(ver/3) + (turn * -turnDirectionConform((mouse.UnitRay.Direction), seat.CFrame))))
		seat.BodyGyro.cframe = CFrame.new(Vector3.new(),g.lookVector - Vector3.new(0,g.lookVector.y,0))
	else
		seat.BodyGyro.cframe = dir *CFrame.fromEulerAnglesXYZ(0, 0, math.rad(ver/3) + (turn * -turnDirectionConform((mouse.UnitRay.Direction), seat.CFrame)))
	end

	local finalCFrame = CFrame.new(Vector3.new(),seat.CFrame.lookVector):toWorldSpace(finalVector)
	--print(#getThrust(seat) .. " / " .. getMass(seat))
	if veri then
		oSpeed = math.min(math.max(oSpeed + veri,0),1)
	
	end
	if oSpeed <= 4 then
	gui.Speed.TextLabel.Size = UDim2.new(oSpeed, 0, 1, 0)
	gui.Speed.TextLabel.Text = "Velocity"
	gui.Speed.TextLabel.BackgroundColor3 = Color3.new(85/255,255/255,0)
	else
		gui.Speed.TextLabel.Size = UDim2.new(math.log(oSpeed)/math.log(4), 0, 1, 0)
		gui.Speed.TextLabel.Text = "Warp Speed"
		gui.Speed.TextLabel.BackgroundColor3 = Color3.new(1,0,0)
	end
	
	local Velocity = finalCFrame.p * (4 + (accountThrusters(getThrust(seat))*4000)/getMass(seat)) * oSpeed
	if not overridePos then
	seat.BodyVelocity.Velocity = seat.BodyVelocity.Velocity:Lerp(Velocity,0.1)
	else
		oSpeed = 600
		seat.BodyVelocity.Velocity = (overridePos - seat.Position) * 18
		if (overridePos - seat.Position).Magnitude < 60 then
			overridePos = nil
			oSpeed = .3
		end
	end
	
	if on then
	seat.BodyGyro.MaxTorque = Vector3.new(400000, 400000, 400000)*3000
	seat.BodyVelocity.MaxForce = Vector3.new(400000, 400000, 400000)*3000
	else
		seat.BodyGyro.MaxTorque = Vector3.new(400000, 400000, 400000)*00
	seat.BodyVelocity.MaxForce = Vector3.new(400000, 400000, 400000)*00
	end
	local sSpeed = oSpeed * 2
	if sSpeed < 40 then
		Thrust1.PlaybackSpeed = sSpeed
	else
		Thrust1.PlaybackSpeed = 2
	end
	print("Velocity: "..oSpeed.."   Thrust playback speed: "..Thrust1.PlaybackSpeed)
	
end
script.Parent.Equipped:connect(function(mouse)
	local cam = workspace.CurrentCamera
	--cam.CameraType = "Attach"
	equipped = true
	mouse.Icon = "http://www.roblox.com/asset/?id=36983768"
	gui.Parent = player.PlayerGui
	gui.Crosshair.Ship.Value = script.Value.Value
	gui.Crosshair.Tilt.Disabled = false
	seat = script.Value.Value
	on = seat.On.Value
	--mouse.TargetFilter = workspace
	game:GetService("UserInputService").InputBegan:Connect(function(key, processed) -- FUNCTION TEST CHANGE
		if processed then return end
		for i, m in pairs(ctrlBind) do
			if m == key then
				ctrlBool[i] = true
			end
		end
		if key.KeyCode == Enum.KeyCode.Q then
			vec = 1
		end
		if key.KeyCode == Enum.KeyCode.E then
			vec = -1
		end
		if key.KeyCode == Enum.KeyCode.H then
			if not fastOn then
				fastOn = true
			else
				fastOn = false
			end
		end
		if key.KeyCode == Enum.KeyCode.V then
			if mo == 0 then
				cam.CameraType = "Track"
				cam.CameraSubject = player.Character.Head
				firing = false
				mo = 1
			else
				cam.CameraType = "Custom"
				cam.CameraSubject = player.Character.Humanoid
				firing = true
				mo = 0
			end
		end
		if key.KeyCode == Enum.KeyCode.C then
			if gui.Controls.Visible then
				for i=8,0,-1 do
					gui.Controls[i].Visible = false
					wait(.05)
				end
				wait(.5)
				gui.Controls:TweenSize(UDim2.new(0,0,0,300),"In","Sine")
				wait(1)
				gui.Controls.Visible = false
			else
				gui.Controls.Visible = true
				gui.Controls:TweenSize(UDim2.new(0,200,0,300),"Out","Sine")
				wait(1)
				for i=0,8,1 do
					gui.Controls[i].Visible = true
					wait(.05)
				end
			end
			wait()
		end
		if key.KeyCode == Enum.KeyCode.T then
			enhancedLocking = not enhancedLocking
		end
		if key.KeyCode == Enum.KeyCode.G then
			if target then
				target = nil
			else
				target = mouse.Target
				if mouse.Target then
					relative = target.CFrame:toObjectSpace(mouse.Hit)
				end
				signal = 120
			end

		end
		if key.KeyCode == Enum.KeyCode.X then
			on = false
			seat.On.Value = on
			Thrust1:Stop()
			if overridePos then
				oSpeed = 1
			end
			overridePos = nil
		end
		if key.KeyCode == Enum.KeyCode.Y then
			on = true
			seat.On.Value = on
			oSpeed = 0.2
			Thrust1:Play()
		end
		if key.KeyCode == Enum.KeyCode.H and warpzone.Value == true then
			if hasFTLTech(seat) and oSpeed < 40 then
				oSpeed = 40
			else
				oSpeed = 1
			end
		end
		if key.KeyCode == Enum.KeyCode.LeftShift then -- proven functional
			veri = 0.006
		end
		if key.KeyCode == Enum.KeyCode.LeftControl then -- proven functional
			veri = -0.006
		end
		if key.KeyCode == Enum.KeyCode.L then
			gyro = not gyro
		end
		if key.KeyCode == Enum.KeyCode.Semicolon then
			oSpeed = 0
		end
	end)
	game:GetService("UserInputService").InputEnded:Connect(function(key, processed) -- FUNCTION TEST CHANGE
		if processed then return end -- this checks if the player is typing in a textbox or pressing a button
		for i, m in pairs(ctrlBind) do
			if m == key then
				ctrlBool[i] = false
			end
		end
		if key.KeyCode == Enum.KeyCode.Q or key.KeyCode == Enum.KeyCode.E then
			vec = 0
		end
		if key.KeyCode == Enum.KeyCode.LeftShift then -- proven functional
			veri = nil
		end
		if key.KeyCode == Enum.KeyCode.LeftControl then -- proven functional
			veri = nil
		end
	end)
	mouse.Button1Down:connect(function()
		local targ = mouse.Hit
		if mouse.Target then
			if mouse.Target.Name == "wormhole" then
				if hasWormholeTech(seat) then
				local whole = mouse.Target
				local dist = mouse.Hit.p - seat.Position
				if dist.Magnitude < 30 then
					local function getSystem(wormhole)
						for i, m in pairs(wormhole:GetChildren()) do
							if m:IsA("Folder") then
								local val1 = m.Value.Value
								local val2 = m.Value2.Value
								local current = wormhole.Parent.Name
								if val1.Name == current then
									return val2
								end
								return val1
							end
						end
					end
					local v=getSystem(whole)
					gui.wGui.Visible = true
					gui.wGui.TextLabel.Text = "Travel to " .. v.Name .. "?"
					
					con = gui.wGui.TextButton.MouseButton1Click:connect(function()
						gui.wGui.Visible = false
						workspace.ModuleScript.PlayerRequestSystem:InvokeServer(v.Value)
						local r = workspace.StarSys:WaitForChild(v.Name)
						print(r)
						local function getClassChild(part,name)
							for i, m in pairs(part:GetChildren()) do
								if m.ClassName == name then return m end
							end
						end
						local function travelTo(wormhole)
							overridePos = wormhole.Position
							oSpeed = 600
						end
						for i, m in pairs(r:GetChildren()) do
							if m.Name == "wormhole" then
								local part = getClassChild(m,"Folder")
								if part.Value.Value.Name == whole.Parent.Name then
									return travelTo(m)
									
								end
								if part.Value2.Value.Name == whole.Parent.Name then
									return travelTo(m)
									
								end
							end
						end
						con:disconnect()
					end)
				end
				end
				
			end
		end
		
	end)
	while equipped do
		updateVectors(cam,mouse,seat)
		wait()
	end
	--seat.BodyGyro.MaxTorque = Vector3.new(400000, 400000, 400000)*0
	--seat.BodyVelocity.MaxForce = Vector3.new(400000, 400000, 400000)*0
end)
gui.wGui.TextButton2.MouseButton1Click:connect(function()
	gui.wGui.Visible = false
	con:disconnect()
	end
)
script.Parent.Unequipped:connect(function()
	local cam = workspace.CurrentCamera
	equipped = false
	cam.CameraType = "Custom"
	gui.Parent = script
	gui.DeadZone.Tilt.Disabled = true
	gui.DeadZone.Ship.Value = nil
	fastOn = false
end)
1 Like