Here is a generalization for how I personally determine this input:
-
a local script in the StarterPlayerScripts detects when the player’s HumanoidState changes to Seated, then it casts a ray downward from the center of the HumanoidRootPart ignoring the character to determine the seat it is sitting on, and the model at large.
-
depending on this model’s name (or some other identifier, such as a variable in the seat), the script will switch on/off banks of inputs. For example, if they sit on a car they can use Headlights, Horn, and traditional WASD movement, however, if they sit on a UFO they can use all of those plus other UFO-specific gadgets.
-
when the player’s state changes to anything away from Seated, disable the banks.
For the banks I have a dictionary set up with smaller dictionaries inside, with each KeyCode defined as true if it is an actionable thing. It looks like:
local vehicleKeyCodes = {
car = {
Enum.KeyCode.G = true, -- horn
Enum.KeyCode.F = true, -- lights
-- other car stuff including WASD
},
ufo = {
Enum.KeyCode.G = true, -- horn
Enum.KeyCode.F = true, -- lights
-- other UFO stuff
},
}
Then I have a variable that is either nil, or the name of the vehicle the player is sitting on, as determined by the ray
local sittingOn = workspace:FindPartOnRay(Ray.new(char.PrimaryPart.Position,Vector3.new(0,-10,0),char)
local vehicleType = sittingOn.VehicleType.Value
– VehicleType is a StringValue object inside the VehicleSeat that contains either “car” or “ufo” or some other vehicle name to reference our the vehicleKeyCode dictionary.
In every VehicleSeat, along with the string value, I have a RemoteEvent that lets the player send their input to the server script. I push the KeyCode through the RemoteEvent and let the server act upon the vehicle. Something like…
UserInputservice.InputBegan:connect(function(input,gp)
if gp then return false end
if sittingOn then
if vehicleKeyCodes[vehicleType].input.KeyCode then
sittingOn.RemoteEvent:FireServer(input.KeyCode)
end
end
end)
– and now in a server script inside the vehicle itself, I say something to the effect of…
seat.RemoteEvent.OnServerEvent:connect(function(player,inputKeyCode)
if inputKeyCode == Enum.KeyCode.X then
-- do a thing on X
elseif inputKeyCode == Enum.KeyCode.Y then
-- do a thing on Y
else
-- blah blah blah
end
end)
And that’s basically it