first issue I see: didTeleport.Value turns false without a confirmed "didTeleport.Vaue = true. This will cause a problem, because it goes through 2 other checks. If it’s a humanoid, and if there’s a rootpart.
second problem (minor): you need to orientate the player’s camera relative to the second portal.
third problem (more minor, might be unimportant): the teleportation method is not very modular.
So the first issue can be solved easily,
local trigger = script.Parent
local cdtime = 1.5
local didTeleport = script.Parent.Parent.Parent.didTeleport
trigger.Touched:Connect(function(hit)
local char = hit.Parent
local hum = char:FindFirstChildWhichIsA("Humanoid")
if hum then
local humRootPart = char:FindFirstChild("HumanoidRootPart")
if humRootPart and didTeleport.Value then
didTeleport.Value = false
humRootPart.CFrame = CFrame.new(-743.258, 122.903, -663.646)
task.wait(cdtime)
didTeleport.Value = true
end
end
end)
second issue is that you need to orientate your camera, luckily i made a portal script before. Put a Remote Event into ReplicatedStorage as you can only edit the Camera by the client.
LocalScript (StarterCharacterScript)
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local camera = workspace.CurrentCamera
RemoteEvent.OnClientEvent:Connect(function(portal, nextPortal)
local cameraCF = camera.CFrame
local cameraPosition = cameraCF.Position
local windowCF = portal.CFrame
local nextWindowCF = nextPortal.CFrame
local relativeOrientation = windowCF:ToObjectSpace(cameraCF)
local newCameraOrientation = nextWindowCF * relativeOrientation
camera.CFrame = CFrame.new(cameraPosition) * CFrame.Angles(newCameraOrientation:ToEulerAnglesXYZ())
end)
Server Script
local Players = game:GetService("Players")
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
trigger.Touched:Connect(function(hit)
local char = hit.Parent
local hum = char:FindFirstChildWhichIsA("Humanoid")
if hum then
local humRootPart = char:FindFirstChild("HumanoidRootPart")
if humRootPart and didTeleport.Value then
didTeleport.Value = false
if Players:GetPlayerFromCharacter(hit.Parent) then
local player = Players:GetPlayerFromCharacter(hit.Parent)
RemoteEvent:FireClient(player, trigger, portal2?) -- add the second portal
end
humRootPart.CFrame = CFrame.new(-743.258, 122.903, -663.646)
task.wait(cdtime)
didTeleport.Value = true
end
end
end)
third issue is what if you want to move your portals? an easy way is to use look vectors!
function to find position
local function teleport(humRootPart, portal2)
local lookVector = portal2.CFrame.LookVector
local distance = 5
local newPosition = portal2.Position + (lookVector * distance)
humRootPart.CFrame = CFrame.new(newPosition)
end
Solved all 3 issues, now we mesh them together!
ServerScript
local trigger = script.Parent
local cdtime = 1.5
local didTeleport = script.Parent.Parent.Parent.didTeleport
local Players = game:GetService("Players")
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local function teleport(humRootPart, portal2)
local lookVector = portal2.CFrame.LookVector
local distance = 5
local newPosition = portal2.Position + (lookVector * distance)
humRootPart.CFrame = CFrame.new(newPosition)
end
trigger.Touched:Connect(function(hit)
local char = hit.Parent
local hum = char:FindFirstChildWhichIsA("Humanoid")
if hum then
local humRootPart = char:FindFirstChild("HumanoidRootPart")
if humRootPart and didTeleport.Value then
didTeleport.Value = false
if Players:GetPlayerFromCharacter(hit.Parent) then
local player = Players:GetPlayerFromCharacter(hit.Parent)
RemoteEvent:FireClient(player, trigger, portal2?) -- add the second portal
end
teleport(humRootPart, portal2?) -- add the second portal
task.wait(cdtime)
didTeleport.Value = true
end
end
end)
LocalScript
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local camera = workspace.CurrentCamera
RemoteEvent.OnClientEvent:Connect(function(portal, nextPortal)
local cameraCF = camera.CFrame
local cameraPosition = cameraCF.Position
local windowCF = portal.CFrame
local nextWindowCF = nextPortal.CFrame
local relativeOrientation = windowCF:ToObjectSpace(cameraCF)
local newCameraOrientation = nextWindowCF * relativeOrientation
camera.CFrame = CFrame.new(cameraPosition) * CFrame.Angles(newCameraOrientation:ToEulerAnglesXYZ())
end)
should be good now