Wrong CFraming, not teleporting players infront of each other

Hello! So I’ve scripted my own admin commands but there is one problem. The To-Command is working wrong. It’s not teleporting the player infront of the other player, more like next to him.

Script:

if splits[1]:lower() == MrCommands["To-Command"] and plr.leaderstats.Rank.Value >= 5 then
			--Find the player
			for i,v in pairs(game.Players:GetChildren()) do
				if v.Name:sub(1, name:len()):lower() == name:lower() then
					--v is the correct player
					local vHumRootPart = v.Character:FindFirstChild("HumanoidRootPart")
					local myHumRootPart = plr.Character:FindFirstChild("HumanoidRootPart")
					myHumRootPart.CFrame = vHumRootPart.CFrame * CFrame.new(10, 0, 0)
				end
			end
		end
1 Like

I’ve also tried doing

myHumRootPart.CFrame = vHumRootPart.CFrame * CFrame.new(0, 0, 10)

which doesn’t work either.

1 Like

You would want to get the characters lookvector and then multiply it by your offset

2 Likes

For some reason this happens for me:
https://gyazo.com/2a6472d3bfc170f5a519be23b8a32422
My script:

myHumRootPart.Position = vHumRootPart.CFrame.LookVector * 10

Did I do something wrong?

1 Like
myHumRootPart.CFrame = vHumRootPart.CFrame * ( vHumRootPart.Cframe.LookVector * 10 ) * CFrame.new( 0 , yOffset , 0  ) 

Where yOffset is the Offset on the yaxis of ethier the Player you are teleporting to or you can try to calculate it another way…

2 Likes

attempt to multiply a Vector3 with an incompatible value type or nil

Script:

myHumRootPart.Position = (vHumRootPart.CFrame.LookVector * 10) * CFrame.new(0 , 5 , 0) 
1 Like

Thats because “Position” is a vector3 value. Make it a CFrame value

2 Likes

I changed it to CFrame but the same error pops up

Script:

myHumRootPart.CFrame = (vHumRootPart.CFrame.LookVector * 10) * CFrame.new(0 , 5 , 0) 
1 Like

Oops I see it :man_facepalming:

We are multiplying a LookVector by a Cframe. LookVector is is a vector3 value

2 Likes

Yep, but we need LookVector to get the direction. How to turn the LookVector into a CFrame?

1 Like
Character:SetPrimaryPartCFrame(YourCFrame)

Hope this helps and pls see the CFrame API:

2 Likes
myHumRootPart.CFrame = myHumRootPart.CFrame * CFrame.new(  myHumRootPart.CFrame.lookVector * 10 ) * CFrame.new(0,yOffset,0)

3 Likes

Script:

	plr.Character:SetPrimaryPartCFrame(v.Character.HumanoidRootPart.CFrame * CFrame.new(5, 0, 0))

Same issue as before, like in the first vid, it doesn’t actually teleport infront of the player.

2 Likes

That doesn’t really work aswell. This is how it looks like:


Script:

	myHumRootPart.CFrame = CFrame.new(  myHumRootPart.CFrame.lookVector * 10 ) * CFrame.new(0, 5 ,0)
1 Like

Here is a simple CFrame calculation.

myHumRootPart.CFrame = vHumRootPart.CFrame + (vHumRootPart.CFrame.LookVector * infrontOffset) * CFrame.fromAxisAngle(Vector3.new(1,0,0),math.rad(180))

First we move FORWARD to them by using LookVector at multiplying it. then we look at them by using CFrame.fromAxisAngle, (this is in radians btw)

3 Likes

Its because your arent offsetting by the other persons humanoid root part Cframe

2 Likes

@Xx1Luffy1xX By the way if you do
LookVector you get a Vector3 and if you do lookVector you get a CFrame??

1 Like

You should only use the LookVector variable for directional/rotational purposes, not for positional purposes. You should use a position reference (and YES, i know that you not are using the LookVector Vector as position reference, only to avoid that people… You know):

plr.Character:SetPrimaryPartCFrame(CFrame.new(v.Character.PrimaryPart.CFrame*Vector3.new(0,0,5)))
2 Likes
myHumRootPart.CFrame = myHumRootPart.CFrame * CFrame.new(  myHumRootPart.CFrame.lookVector * 10 ) * CFrame.new(0,yOffset,0)

This was my whole line of code your forgot to offset it by the HumRootPartCFrame.
And the lookVector returns a Unit Vector in the direction the player is looking at in this case

2 Likes
myHumRootPart.CFrame = v.HumRootPart.CFrame * CFrame.new(0, 0, -10)

Sets your cframe to the same as your target but 10 studs in front relative to their cframe.
You can also multiply it by CFrame.Angles(0, math.pi, 0) so that you are facing your target when you teleport.

3 Likes