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
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.
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
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.
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.