Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Checking whenever a player sits on a Seat part, is there a better way to get that player?

Asked by 4 years ago

Whenever the script.Parent (SeatPart) has a child added, I get the SeatPart's SeatWeld and get that weld's Part1. If that part's Parent is a character of a player, it prints "A player sat down!". Is there a better way to define the player that sat down on the part?

Here's the code:

script.Parent.ChildAdded:Connect(function(part)
    print("Sat down!")
    local part = script.Parent.SeatWeld.Part1
    local plr = part.Parent
    if game.Players:GetPlayerFromCharacter(plr) then 
        print("A player sat down!")
    end
end)

1 answer

Log in to vote
1
Answered by
Prestory 1395 Moderation Voter
4 years ago

You can access the player who is sitting on the seat through the Occupant value which is located inside the seat like i have done below

local Seat = script.Parent

function SitDetection(Child)
    if Child.Name == "SeatWeld" then
        local Character = Seat.Occupant.Parent
        local Player = game.Players:GetPlayerFromCharacter(Character)
        print(Player.Name)
        print(Character.Name)
    end
end

Seat.ChildAdded:Connect(SitDetection)
Ad

Answer this question