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

UserInputService Key Press Not Working, What To Do?

I have put this into a local script which is in seat. Nothing happend when I tried this on Play Solo Mode.

script.Parent.ChildAdded:connect(function(newChild)
	if newChild.Name == "SeatWeld" then
		if UserInputService:IsKeyDown(97) == true then
			print("Pressing A Key")
		end
	end
end)

But when I put this to a normal script:

print(game:GetService("UserInputService"):IsKeyDown(97))

It does work. Any way to fix this?

Thanks,

SwaggyDuckie

1 answer

Firstly, a new API update from ROBLOX allows you to easily check if the player is sitting in a seat with a new property in the Humanoid: SeatPart.

As for your problem, try using the InputChanged event. You can then use the KeyCode property as the event returns an InputObject

script.Parent:WaitForChild("Humanoid").Changed:connect(function() --Wait for the humanoid before connecting the Changed event to our function.
	if script.Parent.Humanoid.SeatPart ~= nil then --If the player is sitting on a chair.
		game:GetService("UserInputService").InputChanged:connect(function(input) --When the player changes their input.
			if input.KeyCode == Enum.KeyCode.A then --If the input key was A. Refer to the KeyCode enum wiki page below.
				print("Key has been pressed")
			end
		end
	end
end)

KeyCode Enum.

You can also use the Seated event of the Humanoid (again, newly added) to check if the player is seated.

script.Parent:WaitForChild("Humanoid").Seated:connect(function(seated) --Wait for the humanoid before connecting the Seated event to our function.
	if not seated then return end --Ends the function if the player isn't seated.
	game:GetService("UserInputService").InputChanged:connect(function(input) --When the player changes their input.
		if input.KeyCode == Enum.KeyCode.A then --If the input key was A. Refer to the KeyCode enum wiki page below.
			print("Key has been pressed")
		end
	end
end)

I hope this answer helped you. If it did, be sure to accept it.

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