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

Keybind script in a vehicle doesn't work. Help?

I'm currently trying to make a system where you an set the direction with Keybinds: R (Forward) F (Neutral) and V (Reserve) Nothing happens. I tried doing print("Test") but It didn't work either.

I'll preferably use normal script but I aslo tried it with local script.

Can anyone help me?

local UIS = game:GetService("UserInputService")
local seat = script.Parent	 
local F = script.Parent.Forward
local N = script.Parent.Neutral
local R = script.Parent.Reverse


while seat.Occupant ~= nil do
	UIS.InputBegan:Connect(function(input, gameProcessedEvent)		
		if input.UserInputType == Enum.UserInputType.Keyboard then 
			local key = input.KeyCode 								
			if key == Enum.KeyCode.R then 					
				if F == false then
					F.Value = true
					R.Value = false
					N.Value = false
				else
					F.Value = false
					R.Value = false
					N.Value = true
				end

			elseif key == Enum.KeyCode.V then
				F.Value = false
				R.Value = true
				N.Value = false
			else
				F.Value = false
				R.Value = false
				N.Value = true
			end

		elseif input.KeyCode == Enum.KeyCode.F then
			N.Value = true
			F.Value = false
			R.Value = false

		end
	end)

	wait(0.1)
end

Thanks in advance

2 answers

✓ Accepted answer

The UserInputService actually is only client side so you will have to use a localscript to get the user input. You can send this information from the client to the server with remotefunctions or remoteevents, and then go from there.

Here's something I threw together as kind of an example of how you could send the input across:

Local Script in StarterPlayerScripts

local UIS = game:GetService("UserInputService")
local seat = game.Workspace:WaitForChild("Seat")
local event = seat.RemoteEvent
local players = game:GetService("Players")
local keycode = Enum.KeyCode
local keys = {keycode.F, keycode.N, keycode.R}

--Send info when input begins
UIS.InputBegan:connect(function(input, processed)--Get input
	if seat.Occupant == players.LocalPlayer.Character.Humanoid then --check if the input should be sent
		if input.UserInputType == Enum.UserInputType.Keyboard then --check if the input is the right type
			for i,v in ipairs(keys) do --find out if the input is one of the right keys
				if input.KeyCode == v then
					event:FireServer(input.KeyCode, "began") --trigger the event for the server
				end
			end
		end
	end
end)
--Send info when input ends
UIS.InputEnded:connect(function(input, processed)
	if seat.Occupant == players.LocalPlayer.Character.Humanoid then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			for i,v in ipairs(keys) do
				if input.KeyCode == v then
					event:FireServer(input.KeyCode, "ended")
				end
			end
		end
	end
end)

Script inside the seat

local event = script.Parent.RemoteEvent
local forward = false
local reverse = false
local neutral = false
function handleinput(player, input, inputtype: string)
	print(player.Name.." "..inputtype.." "..tostring(input))
	if input = Enum.KeyCode.F then
		if inputtype = "began" then
			forward = true
		else
			forward = false
		end
	elseif input = Enum.KeyCode.R then
		if inputtype = "began" then
			reverse = true
		else
			reverse = false
		end
	else
		if inputtype = "began" then
			neutral = true
		else
			neutral = false
		end
	end
end

event.OnServerEvent:Connect(handleinput)

From these I bet you could figure out a way to make it work with what you had going, hopefully this will help!

Edit: I edited the script portion to show you how you could setup your bool values. I haven't run this myself and typed it directly in here so hopefully I didn't mistype anything.

3 comments

0

Thanks but how do I make it activate a bool value when pressing the key?

0

Instead of printing in the script inside the seat you can have it set your bool values, and then set them to false when the key gets released again. I'll edit my answer so you can see what I mean.

0

That somehow doesn't work either, even as a local script.

Ok I tried this as a normal script and as a local script, both of them didn't work.

local event = script.Parent.RemoteEvent
local forward = false
local reverse = false
local neutral = false
function handleinput(player, input, inputtype: string)
	print(player.Name.." "..inputtype.." "..tostring(input))
	if input == Enum.KeyCode.R then
		if inputtype == "began" then
			forward = true
		else
			forward = false
		end
	elseif input == Enum.KeyCode.V then
		if inputtype == "began" then
			reverse = true
		else
			reverse = false
		end
	elseif input == Enum.KeyCode.F then
		if inputtype == "began" then
			neutral = true
		else
			neutral = false
		end
	end
end

event.OnServerEvent:Connect(handleinput)

local F = script.Parent.Forward
local N = script.Parent.Neutral
local R = script.Parent.Reverse

if forward == true then
	F.Value = true
elseif forward == false then
	F.Value = false
end

if neutral == true then
	N.Value = true
elseif forward == false then
	N.Value = false
end

if reverse == true then
	R.Value = true
elseif forward == false then
	R.Value = false
end

3 comments

0

This script should be a normal script, not local. Do you have the local script firing the remoteevent? If you do then the next problem I see it that when you set the values in the lower section you only set them once when the script first runs, so you should wrap that section in a while loop. I would probably only run the loop while the seat has an occupant.

0

You could also just put the part where you change the values inside of the handleinput function instead of doing it separately.

0

I got it!!!! Thank you very much. This helped me a lot.

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