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!
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.
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.
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.
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.
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.