Targeting a local script to the occupant of a seat

Topic – Local Scripts

First dev forum post so sorry if I do something wrong))

Hello,

I’ve been wondering for a while now, how do I target a local script to be… I guess only active for a certain person; My “Example” or “Problem” is with the occupant of a seat.
(If someone outside of the seat hits a “Key bind”, it wont fire the event. But if someone sitting in the seat hits the key it’ll fire the remote event)

Thanks,

–Luke

Yes you’d need to use RemoteEvents in this Instance, my guess is that you’re wanting to detect the Occupant of whoever’s sitting? :thinking:

For the keybind, you could either use a ProximityPrompt inside the Seat or UserInputService

--LocalScript
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Seat = workspace.Seat
local Prompt = Seat.ProximityPrompt

Prompt.Triggered:Connect(function()
    Event:FireServer()
end)
--ServerScript

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Seat = workspace.Seat
local Prompt = Seat.ProximityPrompt
local SittingPlayer

Event.OnServerEvent:Connect(function(Player)

    if SittingPlayer == nil then
        SittingPlayer = Player
        Seat.Occupant = Player.Character.Humanoid
        Prompt.Enabled = false
    end
end)

Maybe something like this?

1 Like

You can use ContextActionService.

-- When the Humanoid enters the seat
ContextActionService:BindAction()
-- When the Humanoid leaves the seat
ContextActionService:UnbindAction()

How exactly can I use it? Could you give me an example?

Thanks

Sorry for the second reply but, I don’t understand the bottom of the server script, could you explain it in notes ?

local function HandleAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin and actionName == "ActionName" then
print("Starting!")
end
end

game.ContextActionService:BindAction("HandleAction", HandleAction, true, Enum.Keycode.A")

Sorry, but I’m sort of a beginner scripter. Could you possibly list some notes on what it does?

local function HandleAction(actionName, inputState, inputObject) -- Creating a function to handle our input
if inputState == Enum.UserInputState.Begin and actionName == "ActionName" then -- Is the User starting to press a key or click the mouse? Is the name of the action equal to the one we want?
print("Starting!") -- If so, print this
end
end

game.ContextActionService:BindAction("HandleAction", HandleAction, true, Enum.Keycode.A) -- Binding our action so we can use it. We set the Action Name, then the Action's function, whether we want it to appear to mobile users or not, and the key we want PC Users to use. (Respectively)

Wheres the part about it only being for the occupant of the seat?

Do you want to do this locally, or on the server?
EDIT: My bad, just re-read your post. You can do this:

Humanoid.Seated:Connect(function()
if Humanoid.Sit == true then
ContextActionService:BindAction() -- Stuff here
else
ContextActionService:UnbindAction() -- More stuff here
print("Not Seated!")

You can fire the RemoteEvent in the ContextActionService function.

Wait, could I just use a prompt

for example: When you trigger the prompt it enables the GUI and the controls?

how could I do that?

-- Server Script
ProximityPrompt.Triggered:Connect(function(Player)
Player.PlayerGui.ScreenGui.Enabled = true
end)
-- Local Script
Humanoid.Seated:Connect(function()
if Humanoid.Sit == false then
Player.PlayerGui.ScreenGui.Enabled = false
end)

Remember to define all the variables!