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.
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
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
-- Player in Vehicle
ContextActionService:BindAction("SoundHorn", SoundHorn, true, Enum.KeyCode.H)
-- Player not in Vehicle
ContextActionService:UnbindAction(SoundHorn)
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?