I have created an engine that moves a vehicle in my game. The problem is in the part of turning the vehicle. When I'm pressing the A and I press the D without releasing the A key, the vehicle does not turn in any direction.
UserInputService.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.A and game.Workspace.MotoVoladora.Seat:FindFirstChild("SeatWeld") then print("Pressing A") game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(12,0,0) end end) UserInputService.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.D and game.Workspace.MotoVoladora.Seat:FindFirstChild("SeatWeld") then print("Pressing D") game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(-12,0,0) end end) UserInputService.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.D and game.Workspace.MotoVoladora.Seat:FindFirstChild("SeatWeld") then print("Not pressing A/D") game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(0,0,0) end end)
From what I've seen in the output, this happens because when I press another key without releasing the first key, the vehicle stops turning after releasing the first key.
Image: https://imgur.com/a/paqRmep
I've been trying for several hours to find a solution without success..
If someone has a problem equal or similar to me, I will leave the solution here.
I created a local function which contains the following:
local function turnVehicle() if counter == 0 then game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(0,0,0) elseif counter == 1 and key == "A-B-1" then game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(12,0,0) elseif counter == 1 and key == "A-B-2" then game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(-12,0,0) elseif counter == 2 then game.Workspace.MotoVoladora.Motor.BodyThrust.Force = Vector3.new(0,0,0) end end
Basically check which key is being pressed with the variable "key" and the number of keys pressed at that moment with the variable "counter".
The function is executed every time you press the key "A" or "D".