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

how to rotate a player to face another player?

Asked by 7 years ago
Edited 7 years ago

i read a wiki and it said to use toObjectSpace. but it didnt work.

player 1 needs to look > towards player 2 player 2 needs to look < towards player 1

local first = CFrame.new(180,0,-180) -- these coordinates were from Studio. 
local second = CFrame.new(0,0,0) -- i typed these in and it faced the character the way i wanted

local playerone= game.Workspace:FindFirstChild(plr.Name)
local playertwo = game.Workspace:FindFirstChild(sender.Value)

playerone.HumanoidRootPart.CFrame:toObjectSpace(first)                                  playertwo.HumanoidRootPart.CFrame:toObjectSpace(second)

what am i doing wrong? it doesn't rotate neither of the players.

no errors either

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
7 years ago

toObjectSpace is a method of the CFrame type which performs a calculation - it doesn't actually modify the object that it was called on. This means that when you use a colon to call that method on a CFrame object, it will actually return a value for you to use.

I'll make a few suggestions about how to get your head around this problem.

Finding the Character

Whilst playerone will point to the Model belonging to plr with your current code on line 4, this is a rather cumbersome and unnecessarily complicated way of getting a reference to the character. You should use this instead:

local playerone = plr.Character -- Character is a property of the Player type

Rotating the Character

By default, ROBLOX does some very fancy manipulation with humanoids and player characters to achieve consistent physics and behaviour across very diverse games. If the player is using mouse lock, first person mode, or they are moving a lot, then setting the CFrame of the HumanoidRootPart will only set the rotation for a very brief moment before the default ROBLOX scripts change it back to something else.

Because of this, you might want to investigate more sophisticated methods for rotating, such as updating rotation with the RenderStepped event of RunService or using BodyGyro to physically rotate the player. If you just want to face the character in an initial direction, then your technique of setting the CFrame directly will do the trick.

Facing Another Player

There is a special constructor of the CFrame type which takes two arguments (pieces of data). The first argument is the starting position, and the second is the position to look towards. If I wanted to make a new CFrame at position a which faces position b I would use:

new_cframe = CFrame.new(a, b)

Since you want to make a player face another, you can make a new CFrame with the starting position as one player and the position to face towards as the other player.

root_part_one = playerone.HumanoidRootPart
root_part_two = playertwo.HumanoidRootPart

-- Face player 1 towards player 2
root_part_one.CFrame = CFrame.new(root_part_one.Position, root_part_two.Position)

-- Face player 2 towards player 1
root_part_two.CFrame = CFrame.new(root_part_two.Position, root_part_one.Position)

I'd suggest having a look at this CFrame tutorial for more information.

0
when you say "default Roblox script" does that mean if they dont move, because they'll be anchored for 5 seconds before they can move.  RobloxianDestory 262 — 7y
0
If you are anchoring the players before setting the rotation then there's no problem. By 'default ROBLOX scripts' I meant the scripts that define player behaviour, which often manipulate the orientation of the player and you sometimes have to be careful to synchronise or override that behaviour when rotating. duckwit 1404 — 7y
0
-- how to make aimbot ChinesePenguware 7 — 6y
Ad

Answer this question