How To Find The Name Of A DriveSeat's Occupant

I’m trying to find the name of the player that sits down in the driveseat. I want it to wait until they’ve sat down on the driveseat, as they won’t spawn in it. So, how can I find the name of the occupant of the driveseat? I have a localscript inside of the driveseat. Like this?:

local Player = DriveSeat.Occupant.Parent

print(Player.Name)

And would I need to have a continuous loop, continually checking if there is an occupant? Like:

while not Player do
	print("Player Not Found!")
	wait(10)
end

if Player then
	print(Player.Name)
end

I’m not sure how to go about this. Any help is appreciated!

I’d need more information in order to help you and would you take a screenshot of the studio explorer with the driveseat, scripts, etc… visible?

When a player sits on a seat, there is a weld that is created and that’s weld’s Part1 would be the Character’s HumanoidRootPart.
image
Here is something of short to show you what I mean since it was hard for me to understand at first.

local seat = YOURSEAT

seat.ChildAdded:Connect(function(obj)
    if obj:IsA("Weld") then
        local CurrentCharacterOccupyingSeat = obj.Part1.Parent --Character model name
    end
end)

seat.ChildRemoved:Connect(function(obj)
    --do stuff 
end)

There is also another way to achieve this.

local seat = YOURSEAT

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if tostring(seat.Occupant) == "Humanoid" then
        local CurrentCharacterOccupyingSeat = seat.Occupant.Parent --Humanoid.Parent
    end
end)

There you go, hope this has been useful to you and countless other people viewing it.

Note: This should be a “Script” if you want it to be visible on the server (the game).

If I am incorrect or you would like to add anything, feel free since I am not the best at this.

1 Like

You do not need a loop to check the occupant.

Every property has a distinct PropertyChangedSignal which can be connected to with the function GetPropertyChangedSignal

An example would be something like this:

part:GetPropertyChangedSignal("Name"):Connect(function()

end)

The same logic can be applied to a VehicleSeat (and a generic Seat for that matter).

First lets connect up the property changed signal through our code:

DriveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()

end)

Very simple, all we’re doing so far is listening for when the Occupant property of DriveSeat is changed and nothing more.

Now we need to add a basic sanity check so that we can identify both that the Occupant is not nil (when a player jumps out of the seat), and that the Occupant is a humanoid (avoiding NPC’s with “Zombie”)

DriveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if DriveSeat.Occupant and DriveSeat.Occupant:IsA("Humanoid") then

	end
end)

Now that that is finished, let’s pull it all together.

DriveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if DriveSeat.Occupant and DriveSeat.Occupant:IsA("Humanoid") then
		local Occupant = DriveSeat.Occupant.Parent
		print(Occupant.Name)
	end
end)

I’m not sure why @uhi_o claims that GetPropertyChangedSignal is inefficient as it is very close to the same thing he is doing (although his method is inherently more hacky), and I’ve not run into any problems when using GetPropertyChangedSignal, neither for Seats or any other Instance.

4 Likes

But some of us don’t use GetPropertyChangedSignal, not to lie.

Explain how it’s more hacky

Because it’s use is not wide spread does not mean it is inefficient, as a matter of fact there is absolutely no correlation between the two - don’t try to link the two qualities to one another when a link simply does not exist.

It’s more “hacky” because there is a built-in method to define the variable, while I don’t use “hacky” in this scenario with the precise definition it is particularly associated with I believe the vocabulary fits my meaning.

Thanks, I ended up following the second method.

@S_maritan thanks for the insight as well!

1 Like

I would agree with @S_maritan. Listening for the addition of the seat weld was the “old” method of finding a player who sat down; now you can do it directly through a property of the seat, rather than going through a separate instance to get to it.