Need help making a CD for the portals and resetting the camera

i couldnt make a simple cooldown for my portals, and I also want to know how do I reset my camera when I teleport, cause when I teleport the camera faces the back, where the portal is, and I want it to face the front, basically where the character is
BY THE WAY I also made a boolvalue for the script, it serves as a true/false debounce, idk if thats effective but i tried it and it kinda works, but found out it breaks, check the video below
id also like to receive criticism regarding my script and tips, tell me everything
https://www.youtube.com/watch?v=nVkpzZ8PAi4 - footage

local trigger = script.Parent
local cdtime = 1.5
local didTeleport = script.Parent.Parent.Parent.didTeleport

trigger.Touched:Connect(function(hit)
	if didTeleport.Value == true then
		didTeleport.Value = false
		local char = hit.Parent
		local hum = char:FindFirstChildWhichIsA("Humanoid")
		if hum then
			local humRootPart = char:FindFirstChild("HumanoidRootPart")
			if humRootPart then
				humRootPart.CFrame = CFrame.new(-743.258, 122.903, -663.646)
				task.wait(cdtime)
				didTeleport.Value = true
			else
				return nil
			end
		end
	end
end)

Снимок экрана 2024-08-22 130922

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

2 Likes

hi, the solution for the first issue works just fine, and the camera problem also works but if shiftlocked it remains same, is that because of shiftlock itself that it fixes itself in place and wont be moved by the scripts?
thanks alot for your effort, youre the best

shift lock is locked to the rotation of the character due to core scripts… so I guess we need to change the camera type.

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local camera = workspace.CurrentCamera

RemoteEvent.OnClientEvent:Connect(function(portal, nextPortal)
    camera.CameraType = Enum.CameraType.Scriptable
	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())

    camera.CameraType = Enum.CameraType.Custom -- restores default camera script
end)

big thanks!!! (character limit)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.