Keybinds for a vehicleseat/seat

Hello, so I was wondering how to setup a keybind event for a vehicleseat/seat. So maybe in a car if you held down the H key, the horn would sound. Or maybe for a steam train if you held down E, a particle emitter would be enabled shooting steam out of a pipe.

Any help would be appreciated, Thanks

8 Likes

Try using UserInputService and a LocalScript that gets run when the VehicleSeat.Occupant is changed. Good luck!!!

7 Likes

Would the .Occupant property even affect anything?

3 Likes

There are many ways to solve the problem of “Is the user currently sitting on the seat?” and that is one of them.

3 Likes

Yes but that isn’t what I am trying to do. I’m trying to have a keybind for if you sit in a seat and you press E, something happens.

2 Likes

Well how do you plan on distinguishing a key press from when you are sitting in a seat vs standing about?

3 Likes

When you only press E while sitting, the car horn sounds

When the player is not seated and pressing E the car horn does not sound

You must put everything in its proper place as pictured for it to function properly

HornCar

ScriptLoadHorn

function powers()
	if script.Parent.Occupant~= nil then
		local Anyone = script.Parent.Occupant
		local player = game.Players:GetPlayerFromCharacter(Anyone.Parent)
		if player ~= nil then
			if Anyone.Parent:findFirstChild("LocalScriptHorn") == nil then
		    local Load = script.LocalScriptHorn:Clone()
			Load.Parent = player.Character
			Load.Disabled = false
			Load.CarValue.Value = script.Parent.Parent
			end
		end
	end
end
script.Parent.Changed:connect(powers)

LocalScriptHorn

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local HornSound = false
local HornEvent = nil

humanoid.Seated:Connect(function(isSeated, seat)
	if isSeated then
		if seat then	
           local Player = game.Players.LocalPlayer
           local mouse = Player:GetMouse()
           mouse.KeyDown:connect(function(key)
	       if key == "e" then
              print("Down E")
			  HornSound = true	
			  HornEvent = script.CarValue.Value.HornEvent
			  HornEvent:FireServer(HornSound)
		   end
           end)
		   mouse.KeyUp:connect(function(key)
	       if key == "e" then
              print("Up E")
			  HornSound = false	
			  HornEvent = script.CarValue.Value.HornEvent
			  HornEvent:FireServer(HornSound)
		   end
           end)
			
		end
    else
		script:Destroy()
    end
end)

ScriptHorn

local HornEvent = script.Parent:WaitForChild("HornEvent")
local Horn = script.Parent:WaitForChild("Horn")
local Effect = script.Parent:WaitForChild("Effect")

local function onHornFired(player, HornSound)
print(player, HornSound)	
	
if HornSound == true then	
Horn.Sound:Play()
Effect.ParticleEmitter.Enabled = true	
end		
		
if HornSound == false then	
Horn.Sound:Stop()
Effect.ParticleEmitter.Enabled = false	
end	
	
end
 
HornEvent.OnServerEvent:Connect(onHornFired)

If it doesn’t work properly, tell me about the problem

I wish you good luck in what you do

12 Likes

Thanks. I’ll take note of everything.

3 Likes

It only works locally, I tried to add a RemoteEvent, but it’s too complicated to do so.

3 Likes

Thank you very much for your note, I fixed the error and it is working on the server now

3 Likes

I suggest using ContextActionService: ContextActionService | Roblox Creator Documentation

This is because you can do something like:

-- Player in Vehicle
ContextActionService:BindAction("SoundHorn", SoundHorn, true, Enum.KeyCode.H)
-- Player not in Vehicle
ContextActionService:UnbindAction(SoundHorn)
4 Likes

Questions regarding the lines of code
Is a script inside the VehicleSeat?
Does it have more lines other than that?
Can other players hear the horn without the Remote?

2 Likes
  1. I haven’t thought about the place it would be, but probably StarterCharacterScripts.
  2. Yes, definitely.
  3. No, it’s a local script.
2 Likes

Quick question, is the effect thing necessary?

2 Likes

No, it’s not effect thing necessary

If you understand the code, you can act with it as you wish

If you have any questions, about the code I can answer them

2 Likes