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

How can i tell if a person is the left or the right of the player?

Asked by 4 years ago

Im making a lock on script, i want to make it so you can press < to make the camera to cycle to a player to the left and > to make the camera cycle to a player to the right of the locked on player. How am i supposed to detect this?

1 answer

Log in to vote
1
Answered by 4 years ago

Vector maths!

Taking player A's torsos as the origin, take the torsos of other players and see if the resulting CFrame/Vector3 is positive or negative on the x (which I assume is the left-right axis). After that, it's a matter of finding the closest player.

For e.g in a local script, this should get players on the left or right of a player.

local Players = game:GetService("Players")
local Player = Players.Player

RightPlayers = {}
LeftPlayers = {}

function PlayersOnRightLeft()
    for _, v in pairs(Players:GetChildren()) do
        local RelativePosition = v.Character.Torso.Position:vectorToObjectSpace(Player.Character.Torso.Position)
        if RelativePosition.X > 0 then
            table.insert(RightPlayers, v)
        elseif RelativePosition.X < 0
            table.insert(LeftPlayers, v)
        end
    end
end

This might not work as I have not tested it, but the idea behind it i.e the method you should be using is foolproof.

Ad

Answer this question