Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect which key is pressed only while a character is in a seat?

Well basically I need to know this to make custom steering

1 comment

0

You're gonna need like Enum.KeyCode.A and Enum.KeyCode.D to steer

1 answer

✓ Accepted answer

Assuming that you're using a LocalScript, Here's your code:

local seat = car.Seat -- change this to your seat

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
if seat:FindFirstChild("SeatWeld") and seat:FindFirstChild("SeatWeld").Part1.Parent.Name == player.Name then
if key == "a" then
-- When the player is sitting and presses a
elseif key == "d" then
-- When the player is sitting and presses d
end
end
end)
mouse.KeyUp:connect(function(key)
if seat:FindFirstChild("SeatWeld") and seat:FindFirstChild("SeatWeld").Part1.Parent.Name == player.Name then
if key == "a" then
-- When the player is sitting and releases a
elseif key == "d" then
-- When the player is sitting and releases d
end
end
end)

3 comments

0

No. KeyDown and KeyUp are deprecated, never opt to use deprecated methods. The modern alternatives are UserInputService and ContextActionService.

0

True.

0

-1. Code is not indented, makes it harder to read. I can reconsider my downvote once the readability of the code is improved.

This discussion is preserved as it was. New answers, comments, and votes are closed.