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)