How to check for inputs from a player while sitting in a seat?

I am trying to create a drum set where when you sit on the seat and click a corresponding key, a sound is played.
Currently having trouble trying to do this, im still a beginner and I can’t find a way to do this from googling.

local seat = script.Parent
local InputService = game:GetService("UserInputService")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	InputService.InputBegan:Connect(function(input)
		
		if input.KeyCode == Enum.KeyCode.A then
			print("clicked A")
		end
		
	end)
end)

Currently, this is the code I have set up but it doesn’t work. I am not entirely sure how to code it to where a function wont end unless the player exits the seat and that it is constantly checking for user inputs.
Apologies if this is a really simple thing! I am still a big noob when it comes to this

2 Likes

Ah I see, I’ll give this a shot and come back later once I get it working! Tysm!

I’m pretty picky, so of course I had to redo a few times.
This is for just that seat by its name.. BSeat in that case.

ServerScript Snippet:

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	State = not State 
	if State then
		local player=Seat.Occupant.Parent
		print(player)
	end
end)
1 Like

You originally stated that to do what I am attempting to do, I need to have a RemoteEvent but I don’t understand where the RemoteEvent is being used here. Do you not have to call it in the script?

I think @2112Jay meant you have two options:

  1. Server script (what you are currently using) with remote event to receive button presses from client
  2. Local script with SeatPart property detection

For option 1, you would have to set up a remote event that fires whenever the player presses a certain button (fires from client). Then, you would have to listen for that remote event on the server when the player is sitting in the seat.

The second option does not require remote events, and the code he gave you (in the first post) should work perfectly. I would recommend you use this option.

Correct me if I am wrong.

1 Like

Ah I see, okay I will attempt this!

This seemed to have worked!

This worked, tysm! I do have a few questions though!

in the following var, why do we have an or that leads to the same class property but with Wait() at the end? After some testing, if i delete player.Character or the var still works but if i delete or player.CharacterAdded:Wait() it breaks. Why is this?
local character = player.Character or player.CharacterAdded:Wait()

Why are we checking for the SeatParts Name? Is it just to make sure 100% we are on the correct seat without having to find it through the explorer using something like game.workspace.seat?

what is the purpose of state if we have debounce? And why exactly are we checking if both are false before going through with the keycode statement?

Why do we check if State = not State instead of State == false?

Sorry if these are kind of noob questions! Thanks again!

This actually can be a bit tricky, and I’m just going to send any key..
You can test for whatever in the ServerScript anyways.

LocalScript
--LocalScript in StarterPlayerScripts

local uis=game:GetService("UserInputService")
local remote=game:GetService("ReplicatedStorage"):WaitForChild("SeatInput")
local player=game:GetService("Players").LocalPlayer

uis.InputBegan:Connect(function(input, db)
	if db then return end --debounce
	
	local character = player.Character
	if not character then return end
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid and humanoid.SeatPart then
		remote:FireServer(input.KeyCode.Name)
	end
end)
ServerScript
--ServerScript in ServerScriptService

local seat = workspace:WaitForChild("BSeat") --BSeat again.
local remote = Instance.new("RemoteEvent", game.ReplicatedStorage)
remote.Name = "SeatInput"

remote.OnServerEvent:Connect(function(player, key)
	local humanoid = seat.Occupant
	if humanoid and key~="Unknown" then --print(key)		
		if key=="A" then
			print("Pressed A")
		end
	end
end)

“Why do we check if State = not State instead of State == false?”
State = not State is a true/false toggle. The first time they sit, it was false, so now it’s true.
When they stand up, it will fire again and toggle back to false. That isn’t an if statement.
It is a direct State change to true or false depending on what it is at the time.

1 Like

Yes, this is what I meant, and both have that GetPropertyChangedSignal, so they don’t fire all the time. I’m trying to answer the question in a way that none of this will ever be a problem again.

1 Like

@2112Jay So after some messing around with the code, I have discovered one bug which I know the cause but not the solution.

In the local script, the following function works up until you exit the seat, in which case, turns the SeatPart.Name == nil

humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	if humanoid.SeatPart.Name == "BSeat" then
		state = not state
	end
end)

The main problem with this is that the script should work but instead of it continuing after nil and toggling the state var, it just ends the script right there.

I have tried a few things but to no avail, possibly you’ll have an idea on how to fix this?

This is just a toggle the outcome is in the InputBegan by not letting get past the state as it now = false.

Even when doing this I still get a bug where when you exit the seat, it still accepts inputs

Maybe I put the code in the incorrect places or pasted it in wrong.

This is the code within StarterPlayer Scripts

local player = game:GetService("Players").LocalPlayer
local inputService = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local state = false

humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	if humanoid.SeatPart.Name=="DrumSeat" then
		state = not state --state toggle 
	end
end)

inputService.InputBegan:Connect(function(input, db)
	if not db and state then --db is a processed debounce
		if input.KeyCode == Enum.KeyCode.A then
			print("Pressed A")
		end
	end
end)

This is the code within the seat

local Seat = script.Parent
local State = false

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	State = not State 
	if State then
		local player=Seat.Occupant.Parent
		print(player)
	end
end)

This is using Occupant, so this has to be a ServerScript.

This is pretty good here, why not use this?

ServerScript
--ServerScript in ServerScriptService
local seat = workspace:WaitForChild("BSeat") -- BSeat again.
local remote = Instance.new("RemoteEvent", game.ReplicatedStorage)
remote.Name = "SeatInput" --make this remote and call it like the other script

remote.OnServerEvent:Connect(function(player, key) --player is always first here
	local humanoid = seat.Occupant if not humanoid then return end
	if humanoid.Parent==player.Character then --print(key)
		
		if key=="A" then
			print("Pressed A")
		end
	end
end)
LocalScript
--LocalScript in StarterPlayerScripts
local uis=game:GetService("UserInputService")
local remote=game:GetService("ReplicatedStorage"):WaitForChild("SeatInput")
local player=game:GetService("Players").LocalPlayer

uis.InputBegan:Connect(function(input, db) if db then return end
	local character = player.Character if not character then return end
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid and humanoid.SeatPart then
		remote:FireServer(input.KeyCode.Name) --player gets passed anyways
end)

All these are double checked.

LocalScript Version

I removed the Solution, It did have an error when they left the seat. :roll_eyes:

--LocalScript in StarterPlayerScripts
local player = game:GetService("Players").LocalPlayer
local inputService = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local state = nil

humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	local seat = humanoid.SeatPart
	state = seat and seat.Name == "BSeat" --true or nil
end)

inputService.InputBegan:Connect(function(input, db)
	if not db and state then

		if input.KeyCode == Enum.KeyCode.A then
			print("Pressed A")
		end
	end
end)