Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Problem with the movement of a vehicle? [SOLVED]

Asked by
Geko97 29
5 years ago
Edited 5 years ago

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..

1
Justo acabo de solucionarlo haciendo un montón de condiciones y comprobaciones, que estoy seguro de que no hacia falta hacer tanto, pero ha funcionado. Lo de IsKeyDown ya lo probé en su momento y no logré solucionarlo. Igualmente muchas gracias ^^ Geko97 29 — 5y

1 answer

Log in to vote
0
Answered by
Geko97 29
5 years ago
Edited 5 years ago

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".

Ad

Answer this question