Vehicle Blinkers/Indicators (Key event)

Hello!

This is my first post so ignore the rough edges.
I’m currently developing a vehicle’s lighting kit
• Blinkers
• Hazzards
• Horn
• Lights
ect…

However I want it so the driver can use keys on his/her keyboard
to operate them. I’ve been in a pickle; searching from forum to forum,
free model to free model, but all of them look either over complicated or
they allow any user, regardless when in the drivers seat, to use the lights.

If anyone could help it would be a Christmas miracle!

Thanks,

E_Link.

4 Likes

welcome to roblox development


It’s a bit quirky, but one way you could do this is to distribute a LocalScript into the character of whoever occupies the seat, and then remove it when they exit the seat.

Using this LocalScript, you can make it connect to a RemoteEvent in the vehicle that the client sends actions to. If you need some guidance on how to use RemoteEvents, just let me know.

I made an example similar to this a couple of weeks ago. This vehicle adds a LocalScript to the occupant that makes the camera shift around while the vehicle is in motion:

Airboat.rbxmx (116.5 KB)

6 Likes

Try this old TRC car.

They were built and scripted by @NWSpacek, open-sourced by @FedoraMasterB98 with permission. I find them pretty easy to understand (mayb because I’ve done something similar b4?)

2 Likes

Thank you I’ll try this. And yes, most scripter’s when they post it public hardly ever put comments explaining what each piece did making it complicated for the next user to learn from :joy: .

Thanks. I’ll check this out.

It’s more of a stylistic approach I guess.

For me personally, I like to be able to actually read my code, so I try to do things as consistently and organized as possible.

1 Like

not really actually

1 Like

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

4 Likes