How to make something happen when a player sits down

How to make something happen when a player sits.

Hey! This is my first tutorial, it’s pretty basic but it should help some people!

The tutorial

Let’s start with defining the seat, this part is optional.

local seat = script.Parent -- Makes a variable for the seat

Now let’s make a :ChildAdded event, if you didn’t know already, every time a player sits down, a weld is created.

local seat = script.Parent

seat.ChildAdded:connect(function(SeatWeld)

end)

Awesome! Now that we have that done, let’s just check if the weld is a weld, and if the weld is a weld, and the name is ‘SeatWeld’.

if SeatWeld.ClassName == "Weld"  and SeatWeld.Name == "SeatWeld" then -- Checks if the it's a weld.

end

Cool, now we know that it’s a weld, we can find the player using :GetPlayerFromCharacter
We can do this by adding… to our script.

local player = game.Players:GetPlayerFromCharacter(SeatWeld.Part1.Parent)

Cool! Now let’s do a quick check-up, to make sure you’re doing this correctly.
Your script should look something like this.

local seat = script.Parent

seat.ChildAdded:connect(function(SeatWeld)
	if SeatWeld.ClassName == "Weld"  and SeatWeld.Name == "SeatWeld" then
		local player = game.Players:GetPlayerFromCharacter(SeatWeld.Part1.Parent)

If it does, well done! You’re following this amazingly.

Anyway, let’s carry on.

OPTIONAL | Debugging

You can add a small wait() to prevent some bugs.

wait(0.0001) -- Debugging

Cool! Let’s now make a :GetPropertyChangedSignal() event to check when the humanoid Sit property changes.

player.Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("Sit"):Connect(function()

Since we have the player, we can now define the Sit property in the humanoid.

local Sit = player.Character.Humanoid.Sit -- We can use our Player variable to find this.

Okay, now that we have all our required checks and variables we need to make this work, let’s make it work!

First of all, let’s check if the player is sitting.

if Sit == true then -- Pretty basic.

Now, let’s check if the player is not sitting.

if Sit == true then
	print("The player just sat down")
    -- Do stuff here when the player sat in the seat.
else
	print("The player is no longer seated.")
    -- Do stuff here when the player got out of the seat.
end

And… you’re done! Make sure to add your end’s and end)s.

			end
		end)
	end
end)
The actual code itself. (for people that can't be bothered to follow tutorial lol)
local seat = script.Parent

seat.ChildAdded:connect(function(SeatWeld)
	if SeatWeld.ClassName == "Weld"  and SeatWeld.Name == "SeatWeld" then
		local player = game.Players:GetPlayerFromCharacter(SeatWeld.Part1.Parent)
		wait(0.0001) -- Debugging
		player.PlayerGui.SongPlayer.Frame.Visible = true
		player.Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("Sit"):Connect(function()
			local Sit = player.Character.Humanoid.Sit
			if Sit == true then
				print("The player just sat down")
			else
				print("The player is no longer seated.")
			end
		end)
	end
end)
11 Likes

There’s dedicated API members you can use to detect when a player sits down as well as check the current seat. You shouldn’t be looking for seat welds, that behaviour could change and a change in the parent or name of the weld will break the script. If the behaviour of sitting changes, up to the weld, that can be reflected in events and such, but not in your code.

If you want to specifically check when the Occupant changes of a seat, use GetPropertyChangedSignal against Seat.Occupant. If you want to perform actions based on when a character sits down rather than against the seat (which, depending on your use case, may be better), then Humanoid.Seated is your go-to. The second parameter is the seat which was sat in. Just like that, you have access to both the character and the SeatPart without involving the players service as well.

ChildAdded will just get fired for any new children parented to the seat which is not what you want to be doing, especially when this script is catered to work only across one seat. Humanoid.Seated will allow you to handle seating logic in a single location, or Occupant changes if it’s relevant to check only if that seat has a player sitting in it. There’s also the concern of SeatWeld changing. Additionally, because you make a connection every time a SeatWeld is added to the seat, your code will cause a memory leak.

Also not quite sure what the point of the wait is in the original code sample. Wait has a minimum waiting time of 1/30th of a second (~0.03 seconds) plus extra so the thread can be fitted into an available slot in the task scheduler to be resumed, so any value lower doesn’t do anything. If you need a wait to “debug” your code, then you’re definitely doing something wrong and need to look into that ASAP.

Simpler code sample that does the exact same thing but better:

local function characterSeated(sittingDown, seatPart)
    if sittingDown then
        print("Player sat down on", seatPart)
    else
        print("Player got up")
    end
end

Humanoid.Seated:Connect(characterSeated)
8 Likes

Oh, well, they both work! But yours is A LOT more easier to understand.

Not trying to sound rude, but I really don’t think this tutorial is necessary to be on the devforum as there already exists an API, and an humanoid Seated event.

The code you provided above isn’t commented nor clear for beginners / other people.

A small tip for your next tutorial here on the devforum, would be to look for actual problems that beginner or advanced developers face, therefore you could show them a solution to that by teaching them your methods.

1 Like

The wait(0.0001) can be replaced with just wait() since the minimum wait time is 0.03 seconds if I recall correctly

1 Like