How to make Portal Spawn in front of Player?

Hello! I’m making a Portal Ability where when the player presses “E”, then a portal will spawn next to them. However, I do not know how to make the portal spawn where the player is facing. Here’s the code;

local RS = game:GetService("ReplicatedStorage")

RS.Teleport.OnServerEvent:Connect(function(plr, pos)
	
	local Entrance = RS.PortalEntrance:Clone()
	local Exit = RS.PortalExit:Clone()
	
	local char = plr.Character
	local torso = char.Torso
	print(pos)
	
	Exit.Position = pos + Vector3.new(0,3.5,0)
	Entrance.Position = torso.Position - Vector3.new(0,0,10)
	Exit.Parent = workspace
	Entrance.Parent = workspace
	
	Entrance.Touched:Connect(function(hit)
		if hit.Parent.Name == plr.Name then
			hit.Parent.HumanoidRootPart.Position = Exit.Position
			for i = 1, 100, 1 do
				Entrance.Position += Vector3.new(0,-0.5,0)
				Exit.Position += Vector3.new(0,-0.5,0)
				task.wait(0.2)
			end
			Entrance:Destroy()
			Exit:Destroy()
		end
	end)
end)
1 Like

I don’t know what the pos argument is but we can use the torso cframe LookVector to find the direction the player is faction. Position = torso.CFrame.LookVector * Vector3.new(0,3.5,0)

Also instead of torso, use HumanoidRootPart

local RS = game:GetService("ReplicatedStorage")

RS.Teleport.OnServerEvent:Connect(function(plr, pos)

	local Entrance = RS.PortalEntrance:Clone()
	local Exit = RS.PortalExit:Clone()

	local char = plr.Character
	local torso = char.HumanoidRootPart
	print(pos)

	Exit.Position = torso.CFrame.LookVector * Vector3.new(0,3.5,0)
	Entrance.Position = torso.CFrame.LookVector * Vector3.new(0 ,0 ,-10)
	Exit.Parent = workspace
	Entrance.Parent = workspace

	Entrance.Touched:Connect(function(hit)
		if hit.Parent.Name == plr.Name then
			hit.Parent.HumanoidRootPart.Position = Exit.Position
			for i = 1, 100, 1 do
				Entrance.Position += Vector3.new(0,-0.5,0)
				Exit.Position += Vector3.new(0,-0.5,0)
				task.wait(0.2)
			end
			Entrance:Destroy()
			Exit:Destroy()
		end
	end)
end)
1 Like

The current position of the portal is (2, 0, 0) relative to the player (called “object space” in Roblox). So, translate it to world space by using :ToWorldSpace.

local position = ...  -- CFrame of the player
local portalRelativePosition = CFrame.new(2, 0, 0)  -- change or rotate as needed

local portalPosition = portalRelativePosition:ToWorldSpace(position)

(Which is actually the same thing as just multiplying the CFrames, but this is more readable in my opinion.)

1 Like