-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local StarterGui = game:GetService("StarterGui")
-- Configuration
local DOUBLE_PRESS_INTERVAL = 0.5 -- Time in seconds to detect double press
local TELEPORT_DISTANCE = 5 -- Distance behind the target
local TELEPORT_COOLDOWN = 0.5 -- Cooldown time in seconds after teleporting
local TELEPORT_DURATION = 0.3 -- Duration of the teleport tween in seconds
-- Variables
local player = Players.LocalPlayer
local lastPressTime = 0
local pressCount = 0
local isCooldown = false
-- Heartbeat Connection for Cooldown
local heartbeatConnection
-- Function to Notify Player
local function notifyPlayer(title, text, duration)
StarterGui:SetCore("SendNotification", {
Title = title,
Text = text,
Duration = duration or 2,
})
end
-- Function to Play Teleport Sound
local function playTeleportSound()
local playerGui = player:WaitForChild("PlayerGui")
local teleportSound = playerGui:FindFirstChild("TeleportSound")
if not teleportSound then
teleportSound = Instance.new("Sound")
teleportSound.Name = "TeleportSound"
teleportSound.SoundId = "rbxassetid://INSERT_YOUR_SOUND_ID_HERE" -- Replace with your desired sound ID
teleportSound.Volume = 1
teleportSound.Parent = playerGui
end
teleportSound:Play()
end
-- Function to Create a Fade In-Out Effect
local function fadeTeleportEffect()
local playerGui = player:WaitForChild("PlayerGui")
local teleportGui = playerGui:FindFirstChild("TeleportGui")
if not teleportGui then
teleportGui = Instance.new("ScreenGui")
teleportGui.Name = "TeleportGui"
teleportGui.ResetOnSpawn = false
teleportGui.Parent = playerGui
end
local fadeFrame = teleportGui:FindFirstChild("FadeFrame")
if not fadeFrame then
fadeFrame = Instance.new("Frame")
fadeFrame.Name = "FadeFrame"
fadeFrame.Size = UDim2.new(1, 0, 1, 0)
fadeFrame.BackgroundColor3 = Color3.new(0, 0, 1) -- Blue overlay
fadeFrame.BackgroundTransparency = 1
fadeFrame.Visible = false
fadeFrame.Parent = teleportGui
end
-- Play Tween Animation
fadeFrame.Visible = true
local tweenIn = TweenService:Create(fadeFrame, TweenInfo.new(TELEPORT_DURATION, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {BackgroundTransparency = 0.5})
local tweenOut = TweenService:Create(fadeFrame, TweenInfo.new(TELEPORT_DURATION, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {BackgroundTransparency = 1})
tweenIn:Play()
tweenIn.Completed:Connect(function()
tweenOut:Play()
end)
tweenOut.Completed:Connect(function()
fadeFrame.Visible = false
end)
end
-- Function to Find the Closest Target (Player or Model with Humanoid)
local function findClosestTarget()
local character = player.Character
if not character then return nil end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return nil end
local closestDistance = math.huge
local closestTarget = nil
local currentPosition = humanoidRootPart.Position
-- Function to Check and Update Closest Target
local function checkAndUpdateTarget(obj)
if obj == character then return end -- Ignore self
local targetHumanoid = obj:FindFirstChildOfClass("Humanoid")
if not targetHumanoid then return end
local targetRootPart = obj:FindFirstChild("HumanoidRootPart") or obj:FindFirstChild("PrimaryPart")
if not targetRootPart then return end
local distance = (currentPosition - targetRootPart.Position).Magnitude
if distance < closestDistance then
closestDistance = distance
closestTarget = targetRootPart
end
end
-- Iterate through Players
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Character then
checkAndUpdateTarget(otherPlayer.Character)
end
end
-- Iterate through Models in Workspace
for _, model in ipairs(workspace:GetDescendants()) do
if model:IsA("Model") and model ~= character then
checkAndUpdateTarget(model)
end
end
return closestTarget
end
-- Function to Smoothly Teleport Behind Target with Correct Facing
local function teleportBehind(targetCFrame, targetName)
if not targetCFrame then return end
-- Calculate Behind Position
local behindOffset = targetCFrame.LookVector * -TELEPORT_DISTANCE
local teleportPosition = targetCFrame.Position + behindOffset
-- Create New CFrame Facing Away from Target's Front
-- This accounts for full 360-degree orientation, including vertical angles
local targetLookVector = targetCFrame.LookVector
local targetUpVector = targetCFrame.UpVector
-- Calculate the direction opposite to the target's LookVector
local desiredLookVector = -targetLookVector
-- Create a new CFrame with the desired position and orientation
local newCFrame = CFrame.new(teleportPosition, teleportPosition + desiredLookVector)
-- Smooth Teleportation using TweenService
local character = player.Character
local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if humanoidRootPart and humanoid then
-- Disable Character Controls and Physics to Prevent Stutter
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
humanoid.AutoRotate = false
-- Create Tween for CFrame
local tweenInfo = TweenInfo.new(TELEPORT_DURATION, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tweenCFrame = TweenService:Create(humanoidRootPart, tweenInfo, {CFrame = newCFrame})
-- Play Tween and Re-enable Controls After Completion
tweenCFrame:Play()
tweenCFrame.Completed:Connect(function(status)
if status == Enum.PlaybackState.Completed then
-- Reset Controls
humanoid.WalkSpeed = 16 -- Default WalkSpeed; adjust if different
humanoid.JumpPower = 50 -- Default JumpPower; adjust if different
humanoid.AutoRotate = true
-- Play Teleport Sound
playTeleportSound()
-- Play Fade Effect
fadeTeleportEffect()
-- Notify Player of Successful Teleport
notifyPlayer("Teleportation", "You have been teleported behind " .. targetName .. "!", 2)
end
end)
end
end
-- Function to Handle Teleportation
local function handleTeleport()
local target = findClosestTarget()
if target then
local targetParent = target.Parent
local targetName = targetParent and targetParent.Name or "Target"
teleportBehind(target.CFrame, targetName)
else
-- Notify Player of No Available Target
notifyPlayer("Teleportation Failed", "No valid target found to teleport behind.", 2)
end
end
-- Function to Start Cooldown Using Heartbeat
local function startCooldown()
isCooldown = true
local elapsed = 0
heartbeatConnection = RunService.Heartbeat:Connect(function(deltaTime)
elapsed = elapsed + deltaTime
if elapsed >= TELEPORT_COOLDOWN then
isCooldown = false
if heartbeatConnection then
heartbeatConnection:Disconnect()
heartbeatConnection = nil
end
end
end)
end
-- Function to Handle Key Press
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.Three then
local currentTime = tick()
if isCooldown then return end
if currentTime - lastPressTime <= DOUBLE_PRESS_INTERVAL then
pressCount = pressCount + 1
else
pressCount = 1
end
lastPressTime = currentTime
if pressCount == 2 then
handleTeleport()
pressCount = 0
startCooldown()
end
end
end
-- Connect Input Listener
UserInputService.InputBegan:Connect(onInputBegan)
-- Reset Cooldown on Character Respawn
player.CharacterAdded:Connect(function(char)
isCooldown = false
if heartbeatConnection then
heartbeatConnection:Disconnect()
heartbeatConnection = nil
end
end)