What’s the best way to detect if someone sat on a specific seat?
Now so far, I’ve seen 2 methods.
1. ChildAdded and ChildRemoving Method
workspace.Seat.ChildAdded:Connect(function(Child)
if Child:IsA("Weld") and Child.Name == "SeatWeld" then
local character = Child.Part1.Parent
local player = game.Players:GetPlayerFromCharacter(character)
print(tostring(player).." has entered the seat!")
-- etc
end
end)
workspace.Seat.ChildRemoved:Connect(function(Child)
if Child:IsA("Weld") and Child.Name == "SeatWeld" then
local character = Child.Part1.Parent
local player = game.Players:GetPlayerFromCharacter(character)
print(tostring(player).." has left from the seat!")
-- etc
end
end)
2. Humanoid.Seated Event Method
local character = localplayer.Character or localplayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Seated:Connect(function(IsSeated, Seat)
if IsSeated and Seat == workspace.Seat then
print(tostring(localplayer).." has entered the seat!")
elseif not IsSeated then
print(tostring(localplayer).." has left from the seat!")
end
end)
Also, do they have any ups or downs?
If there’s any other topic relating to this please let me know!
game.Workspace.Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local humanoid = game.Workspace.Seat.Occupant
local char = humanoid.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
if game.Workspace.Seat.Occupant then
print(tostring(plr).." has entered the seat!")
else
print(tostring(plr).." has left from the seat!")
end
end)
But, my else statement gives me the following error:
Does Occupant not detect the humanoid before leaving?
Well there’s multiple ways of approaching this. One way is to do this for a specific seat
local seat = workspace.Seat
seat:GetPropertyChangedSignal('Occupant'):Connect(function()
if seat.Occupant ~= nil then
local player = game:GetService('Players'):GetPlayerFromCharacter(seat.Occupant.Parent)
print(player.Name..' has entered the seat.')
end
end)
Another way would be to use a local script in StarterCharacterScripts and everytime the humanoid’s SeatPart changes, you can fire a RemoteEvent to the server.
Seat.Occupant is ‘nil’ if the seat has no occupant. Seat.Occupant.Parent would error in cases where a seat has no occupant as ‘nil’ has no member named ‘Parent’.
local player
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
local occupant = script.Parent.Occupant
if occupant then
player = occupant
end
end)
Essentially this code will see if there’s an occupant and if there is an occupant then it just sets “player” to be said occupant.
That should be the last person to sit in the seat.