local PlayerSitting = nil
local function SitPlayerInSeat(Seats)
Seats:GetPropertyChangedSignal("Occupant"):Connect(function()
PlayerSitting = Seats.Occupant
if PlayerSitting then
print('ok')
else
PlayerSitting.CFrame == --place, BUT PLAYERSITTING IS NIL!!!--
end
end)
end
for _, Seat in pairs(CollectionService:GetTagged("Seats")) do
SitPlayerInSeat(Seat)
end
How would I achieve getting the player after they already sat down? (This is in a server script).
local Players = game:GetService("Players")
local seat = Instance.new("Seat")
seat.Anchored = true
seat.Position = Vector3.new(0, 1, 0)
seat.Parent = workspace
local currentPlayer = nil
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local humanoid = seat.Occupant
if humanoid then
local character = humanoid.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
print(player.Name.." has sat down")
currentPlayer = player
return
end
end
if currentPlayer then
print(currentPlayer.Name.." has got up")
currentPlayer = nil
end
end)
When a character sits down, the character’s root and the seat get welded together. This results in there being a weld parented to the seat.
With this information, we can now check for when a child is removed from the seat - which happens when the character jumps out of the seat.
local Seat = script.Parent
Seat.ChildRemoved:Connect(function(Child)
if Child:IsA("Weld") then
print(Child.Part1.Parent)
end
end)
In the code snippet above, Child.Part1 is the HumanoidRootPart. The parent would then refer to the player’s character.
Moving on from this, we can get the Player instance by calling the Players:GetPlayerFromCharacter() function.
local Seat = script.Parent
Seat.ChildRemoved:Connect(function(Child)
if Child:IsA("Weld") then
local Player = game.Players:GetPlayerFromCharacter(Child.Part1.Parent)
print(Player)
end
end)
local seat = -- your seat
local player = nil
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant then
local humanoid = seat.Occupant
player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
else
end
end)