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

How do I turn on the lights/horn of my car with a keybind?

I am trying to active the lights and the horn of a car upon a key press and it isn't working. Script inside of VehicleSeat:

local light1 = script.Parent.Parent.Lights.Main.Light1.SpotLight
local light2 = script.Parent.Parent.Lights.Main.Light2.SpotLight
local lightson = false
local seat = script.Parent

while seat.Occupant ~= nil do
	local mouse = game.Players[seat.Occupant.Parent.Name]:GetMouse()
	mouse.KeyDown:connect(function(key)
		if key == "z" then
			if lightson == false then
				lightson = true
				light1.Enabled = true
				light1.Parent.Material = "Neon"
				light2.Enabled = true
				light2.Parent.Material = "Neon"
			else
				lightson = false
				light1.Enabled = false
				light1.Parent.Material = "SmoothPlastic"
				light2.Enabled = false
				light2.Parent.Material = "SmoothPlastic"
			end
		elseif key == "x" then
			script.Parent.Horn:Play()
		end
	end)
	mouse.KeyUp:connect(function(key)
		if key == "x" then
			script.Parent.Horn:Stop()
		end
	end)
end

1 answer

Well, KeyDown and KeyUp is deprecated by Roblox, so they won't work. Instead, try using UserInputService. Also, this service is only available client-side

This is how your script might look.

-- The locations are taken, assuming it is Local Script
-- If it isn't a local script, do change the locations accordingly, also, keep the Local Script in StarterPlayerScripts

local light1 = script.Parent.Parent.Lights.Main.Light1.SpotLight
local light2 = script.Parent.Parent.Lights.Main.Light2.SpotLight
local lightson = false
local seat = script.Parent
local UIS = game:GetService("UserInputService")

while seat.Occupant ~= nil do
	UIS.InputBegan:Connect(function(input, gp) -- Triggers when an input is detected
		if input.UserInputType == Enum.UserInputType.Keyboard then -- Checks whether the input is from the Keyboard
			local key = input.KeyCode -- Gets the key pressed
	        	if key == Enum.KeyCode.Z then -- Checks whether the pressed key is 'Z'
	            		if lightson == false then
	                		lightson = true
	                		light1.Enabled = true
	                		light1.Parent.Material = "Neon"
	                		light2.Enabled = true
	                		light2.Parent.Material = "Neon"
	            		else
	                		lightson = false
	                		light1.Enabled = false
	                		light1.Parent.Material = "SmoothPlastic"
	                		light2.Enabled = false
	                		light2.Parent.Material = "SmoothPlastic"
	           		end
	        	elseif key == Enum.KeyCode.X then
	            		script.Parent.Horn:Play()
	        	end
		end
    	end)
    	UIS.InputEnded:Connect(function(input, gp) -- Triggers when an input is released
		if input.UserInputType == Enum.UserInputType.Keyboard then -- Checks whether the input is from the Keyboard
			local key = input.KeyCode -- Gets the key released
        		if key == Enum.KeyCode.X then
            			script.Parent.Horn:Stop()
        		end
		end
    	end)
wait(0.1) -- Adding a wait to prevent exhaustion
end

Lemme know if it helps!

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