How to run a script when the player is sitting in a seat?

Id like to convert my script to work when the player sits in a seat.

I’m making a game where the player sits in a seat, and a whole sequence occurs where they’re locked in a chair and teleported to a random place of my choosing through a sort of dream sequence. I’m a builder, not a scripter and I barely know anything about scripting. However through frankenstein-ing other scripts together I’ve gotten something to work. Basically the player touches a trigger which starts the sequence as they sit in the chair. I’d like for the sequence to trigger when the player is in the chair and have the trigger be completely gone so its just the seat itself.

Id like to remove the trigger because the player can trigger the system without sitting in the chair, ruining the whole sequence.

I have 3 scripts that trigger when the player touches a part, one that works some SFX, one that removes the hair of the player, and one that locks them in and teleports them. I’m sure I could probably make all these way easier with one script but I know nothing.

I’ve tried using other scripts as references for mine but nothing has seemed to work.

I’m going to provide some videos and images of the code and system, and if anyone has any recommendations for how I could get this to work it would be a great help!

(Note: I know the teleport fails because I’m in studio, that isn’t an issue.)

Screenshot 2023-08-09 162912

Screenshot 2023-08-09 163441

Screenshot 2023-08-09 163449

I just realized I only provided lines 9 - 36 on the SFX Script, sorry.

I think you can try detecting when the property Occupant for seats changes, as this property will point to the humanoid of the Player sitting on a specific seat. Something like:

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
   if seat.Occupant then
     -- Your code here.
   end
end)
3 Likes

The humanoid object of characters have a Seated event which returns the state: true when sitting, false when jumping out of the seat. I normally access the event and use the event this way for client and server interactions, however @Deb0rahAliWilliams provided a good use for server interaction

local cutscenefunction(player)
    —whatever your cutscene does
end

local function onPlayerAdded(player)
    local SeatConnect = nil
    player.CharacterAdded:Connect(function(character)
        SeatConnect = character.Humanoid.Seated:Connect(function(state, seatPart)
            if state == true and seatPart == [[whatever reference to a seat you have]] then
                cutscenefunction(player)
            else return end
        end
    end
end
game.Players.PlayerAdded:Connect(onPlayerAdded(player))

This method is also useful for if you have multiple of these special seats, they only need to exist under the same name as the one inside the script (with a small modification)

4 Likes

Both of these replies are very useful, thank you!

I’ll let you know if there’s any other issues while I try to work this out.

3 Likes

you can add a non collidable transparent part almost the exact position as the seat but one stud up and add a script when part is touched activate the script.

2 Likes