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

How to tell if the occupant of a seat is pressing a key?

I have this code:

local Plr = game.Players.LocalPlayer
function onKeyPress(inputObject, gameProcessedEvent)
	if script.Parent.Occupant.inputObject.KeyCode == Enum.KeyCode.E then
	game.ReplicatedStorage.notification:FireClient(Plr, "test!")
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)


but when I sit in the seat and press E, nothing happens, please help.

Extra info:It's an anchor system for a ferry kit, so I'm going to have a notification system that shows up on the person in the seat's screen.

2 answers

✓ Accepted answer

This is supposed to be a LocalScript since you're using Players.LocalPlayer, which in this case, you can have the notification fire locally without having to use RemoteEvents. If you want to fire the server for some reason, use ReplicatedStorage.notification:FireServer(argument).

local plr = game:GetService('Players').LocalPlayer
repeat wait() until plr and plr.Character and plr.Character:FindFirstChild('Humanoid')
local char,hum = plr.Character,plr.Character.Humanoid

local UIS = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

UIS.InputBegan:Connect(function(input,event)
	if input.KeyCode == Enum.KeyCode.E and hum.Sit == true then
		if hum.SeatPart ~= nil and hum.SeatPart.Name == 'SomeSeatName' then
			print('you're sitting on a specific seat!")
			--this returns the Part itself, so you can check if it's a specific color or such
		end
		--not sure what you're doing here, but you can't fire client from a local script
		--make this notification happen on the client instead of through an event
		print("you're sitting and pressing a key!")
	end
end)

2 comments

0

This works in every seat, so anybody can anchor the ship if they're sitting in a seat, is there a way to edit this so that if they're sitting in a specific seat, and only a specific seat it works?

0

i made an edit

Instead of using onkeypress try using a

kayDown()

function

1 comment

0

I don't think this makes a difference, I tried and nothing happened.

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