Getting player name

I want to get the name of a player that sits infront of you

Explanation:
Player 1 infront of player 2

So I want to get the name of player 1 or any character that sits infront of you

Assuming you’re using Seats, you can use Players:GetPlayerFromCharacter(Seat.Occupant.Parent) to get the player in a seat. (Seat.Occupant is the Humanoid that’s sitting in a seat, so Seat.Occupant.Parent is their character.)

For example:

local Players = game:GetService("Players")

local seat = workspace.FirstSeat
local otherSeat = workspace.SecondSeat

-- logic or event connections...

if seat.Occupant and otherSeat.Occupant then
    local seatPlayer = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
    local otherSeatPlayer = Players:GetPlayerFromCharacter(otherSeat.Occupant.Parent)
    if seatPlayer and otherSeatPlayer then  -- Humanoids aren't always players!
        print(otherSeatPlayer.Name .. " infront of " .. seatPlayer.Name)
end

This is what I meant.for example,the dummy1 is me.so I want to get a player or dummy that are infront of me

In that case, you should use raycasts to see if there is a character in front of another character (i.e. raycast from their HumanoidRootPart/Character.PrimaryPart and see if it collides with another one, then use similar logic to my first reply to get the player name). Raycasts allow you to use rays to check for collisions in a certain direction. If you haven’t used them before, I’d recommend checking out the Intro to Raycasting article on the DevHub for some usage examples.

1 Like