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

How To Find Player From Vehicle?

Asked by 10 years ago

I have a series of code that detects a vehicle, this code is irrelevant. What does matter is that once I have found the vehicle I need to find the driver of said vehicle and then the players leader stats. I have no idea of how to do this so any help or ideas are much appreciated.

The extra bits. Firstly the script is in a button which is defined in the script below as theobject. It works and prints yay but after then (bit finding vehicle owner) does not work. There are also various scripts in the Vehicle seat if that effects the code here with function(child) bit.

Heres the code:

local v1 = game.Workspace.Vehicle1
local theobject = script.Parent

theobject.Touched:connect(function(p)
if p.Parent == v1 then  
print ("Yay")
    v1.VehicleSeat.ChildAdded:connect(function(child)
        if not child:IsA("Weld") then return end
        if not child.Parent or not child.Part1 or not child.Part1.Parent then return end
        local character = child.Part1.Parent
        local player = game.Players:playerFromCharacter(character)
        if not player then return end
        print (":D")
        print (player)
    end)

ting = 0 --remove debounce

end
end)
0
Are you using the same code that I posted here? https://scriptinghelpers.org/questions/1183/vehicle-camera-focuses-on-part if so, I'll edit my post there to do what you want nate890 495 — 10y
0
If it is possible to assign the vehicle to a player then yes, please. skunk940 10 — 10y

2 answers

Log in to vote
1
Answered by 10 years ago

When a player sits in a seat, a Weld object is created inside of the seat which welds the Character to the seat. You can use the Part0 and Part1 properties to find the Character, and from the Character you can get the Player with the GetPlayerFromCharacter method of the Players service.

Here's a code snippet that you can put into the seat to find the player who sat down:

script.Parent.ChildAdded:connect(function(child)
    if not child:IsA("Weld") then return end
    --^ We only care if there was a Weld inserted into the seat

    if not child.Parent or not child.Part1 or not child.Part1.Parent then return end
    --^ Make sure the Weld wasn't removed immediately (can happen sometimes)
    --& Make sure the weld's Part1 property is set
    --& Make sure the weld's Part1 has a parent as well

    local character = child.Part1.Parent
    --^ The character is the parent of Part1
    local player = game.Players:playerFromCharacter(character)
    --^ We use the playerFromCharacter method of Players to grab the player
    if not player then return end
    --^ player will be nil if the figure sitting in the chair isn't a Player

    print(player.Name .. " is sitting in the seat!")
end)
0
It's not working for what i want. Porberly a edit i have to make. I ahve edited the question with the script i have and more details. skunk940 10 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
theobject.Touched:connect(function(p)
if p.Parent == v1 then --Checks if the part's parent is the vehicle model, if parts that you want to be detected are in a model inside the vehicle model, ungroup or add an or statement before "then" (or p.Parent.Parent == v1).
    local child = v1.VehicleSeat.SeatWeld
    local character = child.Part1.Parent
    local player = game.Players:playerFromCharacter(character)
    if not player then return end
    print(player.Name)
    print(player)
end

We know that it's v1 so we can go though v1 to the seatweld. Then you can find part1 from that and the rest is easy.

Answer this question