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

How to make player teleport in front instead of beside?

Hello developers!

I made a working teleport command and currently it teleports you +3 away from the player on the X axis. But I want it to teleport the player right in front of the player who called the command. Like this:

https://ibb.co/DKKzL2p

And here is my script:

local function findplayer(name)
	
	print("started process")
	for i,v in pairs(game.Players:GetPlayers()) do
print("looping")
		local name1 = string.lower(v.Name)

		if string.sub(name1, 1, string.len(name)) == name then
			print("found player")
return v
		
end
		
	end
	end
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
local function handler(message, plr)
	print("function")
	local prefix = ":"
	local spacer = " "
	local lower = string.lower(message)
	local args = string.split(lower, spacer)
	local cmd = string.split(args[1], prefix)
	if cmd[2] == "tp" then	
		print("tp")
		if args[2] and args[3] then
			
	
				print("cmd aloud")
			local char3 = game.Workspace[(findplayer(args[2])).Name]
				print(char3)
			local char4 = game.Workspace[(findplayer(args[3])).Name]
				print(char4)
			char3.HumanoidRootPart.CFrame = CFrame.new(char4.HumanoidRootPart.CFrame.X-3,char4.HumanoidRootPart.CFrame.Y,char4.HumanoidRootPart.CFrame.Z+3)
			print("done")
			end
end
end




game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(message)
		print("chatted")
		handler(message, plr)
	end)
end)

game.ReplicatedStorage.Get.OnServerEvent:Connect(function(plr, message) -- receives here
	print("received")
	handler(message, plr)
end)

Thank you in advance! :)

1 answer

To position a part relative to another part with CFrames, you should use the :ToWorldSpace method.

In your case:

Change

char3.HumanoidRootPart.CFrame = CFrame.new(char4.HumanoidRootPart.CFrame.X-3,char4.HumanoidRootPart.CFrame.Y,char4.HumanoidRootPart.CFrame.Z+3)

to

char3.HumanoidRootPart.CFrame = char4.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0,0,-3))

To understand CFrame manipulation more, visit this page of the Roblox Developer Website

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