How to teleport an object in front of a player

Hey everyone! I’m trying to make an object clone from ReplicatedStorage to in front of the player, but the script I’m using doesn’t work correctly, and I’m not sure why:

image
Object always teleport to this same position

Here’s the script I’m using (inside StartPlayerScript)

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() or  player.Character
local InputService = game:GetService("UserInputService")

local head = character:WaitForChild("Head").CFrame
local pPos = head.LookVector
local pDistance = 3
local PortalPos: Vector3 = head.Position + (pPos * pDistance)

local debounce = false

InputService.InputBegan:Connect(function(InputKey, Processed)
	if InputKey.KeyCode == Enum.KeyCode.E then
		game.ReplicatedStorage.TimePortal.Position = PortalPos
		game.ReplicatedStorage.TimePortal:Clone().Parent = game.Workspace
		print("cloned")
	end
end)

try this code

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() or  player.Character
local InputService = game:GetService("UserInputService")

local head = character:WaitForChild("Head").CFrame
local pPos = head.LookVector
local pDistance = 3
local PortalPos = head.Position + Vector3.new(pPos * pDistance)

local debounce = false

InputService.InputBegan:Connect(function(InputKey, Processed)
	if InputKey.KeyCode == Enum.KeyCode.E then
		game.ReplicatedStorage.TimePortal:Clone(game.Workspace).Position = PortalPos
		print("cloned")
	end
end)
2 Likes

What you need is CFrame instead of Position.

local TimePortal : Instance = ...
local NewTimePortal = TimePortal:Clone()

NewTimePortal.CFrame = Character:GetPivot() + CFrame.new(0, 0, 3)
-- or if the portal is a model which requires a PrimaryPart
NewTimePortal:PivotTo(Character:GetPivot() + CFrame.new(0, 0, 3))
1 Like

You are changing the position of the portal in ReplicatedStorage instead of doing so after cloning it. Try with:

InputService.InputBegan:Connect(function(InputKey, Processed)
	if InputKey.KeyCode == Enum.KeyCode.E then
        local Portal = game.ReplicatedStorage.TimePortal:Clone(game:GetService("Workspace"))
        Portal.Position = PortalPos
        print("Cloned")
	end
end)
1 Like

Doesn’t work. The portal isn’t even cloned, but “cloned” is printed

This is the only method that worked, but there’s one problem, like, the portal doesn’t face the player’s front, it’s always at the same orientation

Similar to this but a little different:

local NewPosition = Character:GetPivot():PointToWorldSpace(Vector3.new(0, 0, 3))

NewTimePortal.CFrame = CFrame.new(NewPosition, Character:GetPivot().Position)

Edit: This would face at the player not the front.

local PlayerCFrame = Character:GetPivot()
local NewPosition = PlayerCFrame:PointToWorldSpace(Vector3.new(0, 0, 3))
local FrontPosition = PlayerCFrame:PointToWorldSpace(Vector3.new(0, 0, 4))

NewTimePortal.CFrame = CFrame.new(NewPosition, FrontPosition)
1 Like

Yeah, It’s facing my back, but just change CFrame.new(0, 0, 3) to CFrame.new(0, 0, -3)
Thank you very much for your help!

1 Like