Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.