Add in a more aesthetic GUI and make it loop teleport on press the start button, and also add in cframe as well as ALWAYS facing the direction they're facing when teleporting.
code
Assistant
-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local ScreenGui = Instance.new("ScreenGui")
local TextBox = Instance.new("TextBox")
local TeleportButton = Instance.new("TextButton")
-- Configuration
local DOUBLE_PRESS_INTERVAL = 0.5 -- Time in seconds to detect double press
local TELEPORT_DISTANCE = 2 -- Distance behind the target
local TELEPORT_COOLDOWN = 0.1 -- Cooldown time in seconds after teleporting
-- 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)
game.StarterGui:SetCore("SendNotification", {
Title = title,
Text = text,
Duration = duration or 2,
})
end
-- Function to Find Target by Username
local function findTargetByUsername(username)
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer.Name:lower() == username:lower() then
return otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart")
end
end
return nil
end
-- Function to 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
local newCFrame = CFrame.new(teleportPosition) * CFrame.Angles(0, math.pi, 0)
-- Teleport the Player
local character = player.Character
local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
humanoidRootPart.CFrame = newCFrame
-- Notify Player of Successful Teleport
notifyPlayer("Teleportation", "You have been teleported behind " .. targetName .. "!", 2)
end
end
-- Function to Handle Teleportation
local function handleTeleport(username)
local target = findTargetByUsername(username)
if target then
local targetName = target.Parent and target.Parent.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 Button Click
local function onTeleportButtonClick()
if isCooldown then return end
local username = TextBox.Text
handleTeleport(username)
startCooldown()
end
-- Setup GUI
ScreenGui.Parent = player:WaitForChild("PlayerGui")
TextBox.Parent = ScreenGui
TextBox.Size = UDim2.new(0, 200, 0, 50) -- Size of the TextBox
TextBox.Position = UDim2.new(0.5, -100, 0, 50) -- Centered on the screen
TextBox.PlaceholderText = "Enter Username"
TextBox.TextColor3 = Color3.new(1, 1, 1) -- White text
TextBox.BackgroundColor3 = Color3.new(0, 0, 0) -- Black background
TeleportButton.Parent = ScreenGui
TeleportButton.Size = UDim2.new(0, 200, 0, 50) -- Size of the Button
TeleportButton.Position = UDim2.new(0.5, -100, 0, 110) -- Below the TextBox
TeleportButton.Text = "Teleport"
TeleportButton.BackgroundColor3 = Color3.new(0, 0.5, 0) -- Green background
TeleportButton.TextColor3 = Color3.new(1 , 1, 1) -- White text
-- Connect Button Click Listener
TeleportButton.MouseButton1Click:Connect(onTeleportButtonClick)
-- Reset Cooldown on Character Respawn
player.CharacterAdded:Connect(function(char)
isCooldown = false
if heartbeatConnection then
heartbeatConnection:Disconnect()
heartbeatConnection = nil
end
end)
Certainly! I'll enhance your script by implementing a more aesthetic GUI, enabling continuous teleportation with a Start and Stop mechanism, incorporating CFrame for precise teleportation, and ensuring the player always faces the direction they're moving when teleporting.
Enhanced Script Features:
Aesthetic GUI Enhancements:
Added background frames with gradients.
Styled buttons with hover effects.
Organized layout for better user experience.
Continuous Teleportation:
Introduced Start and Stop buttons to control the teleportation loop.
Implemented a loop that continuously teleports the player behind the target at specified intervals.
Precise Teleportation with CFrame:
Utilized CFrame to maintain the player's facing direction relative to the target.
Maintaining Player’s Facing Direction:
Calculated the new CFrame based on the target's facing direction to ensure the player consistently faces the same direction upon teleporting.
Below is the updated and enhanced script:
lua
-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService") -- For GUI animations
-- Configuration
local TELEPORT_INTERVAL = 0.5 -- Time in seconds between teleports
local TELEPORT_DISTANCE = 5 -- Distance behind the target
local GUI_THEME = {
BackgroundColor = Color3.fromRGB(30, 30, 30),
ButtonColor = Color3.fromRGB(45, 45, 45),
ButtonHoverColor = Color3.fromRGB(60, 60, 60),
TextColor = Color3.fromRGB(255, 255, 255),
AccentColor = Color3.fromRGB(0, 170, 255)
}
-- Variables
local player = Players.LocalPlayer
local teleporting = false
local teleportLoopConnection
local targetCharacter = nil
-- Function to Notify Player
local function notifyPlayer(title, text, duration)
game.StarterGui:SetCore("SendNotification", {
Title = title,
Text = text,
Duration = duration or 2,
})
end
-- Function to Find Target by Username
local function findTargetByUsername(username)
local targetPlayer = Players:FindFirstChild(username)
if targetPlayer and targetPlayer.Character and targetPlayer.Character:FindFirstChild("HumanoidRootPart") then
return targetPlayer.Character
end
return nil
end
-- Function to Teleport Behind Target with Correct Facing
local function teleportBehind(targetCharacter)
if not targetCharacter then return end
local targetHRP = targetCharacter:FindFirstChild("HumanoidRootPart")
if not targetHRP then return end
-- Calculate Behind Position
local offset = targetHRP.CFrame.LookVector * -TELEPORT_DISTANCE
local teleportPosition = targetHRP.Position + offset
-- Maintain Player’s Facing Direction
local playerHRP = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if playerHRP then
local currentLookVector = playerHRP.CFrame.LookVector
local newCFrame = CFrame.new(teleportPosition, teleportPosition + currentLookVector)
playerHRP.CFrame = newCFrame
end
end
-- Function to Handle Teleportation Loop
local function startTeleportLoop(username)
if teleporting then return end
targetCharacter = findTargetByUsername(username)
if not targetCharacter then
notifyPlayer("Teleportation Failed", "No valid target found.", 2)
return
end
teleporting = true
notifyPlayer("Teleportation Started", "Continuously teleporting behind " .. username .. ".", 2)
teleportLoopConnection = RunService.RenderStepped:Connect(function()
if not teleporting then return end
teleportBehind(targetCharacter)
end)
end
local function stopTeleportLoop()
if not teleporting then return end
teleporting = false
if teleportLoopConnection then
teleportLoopConnection:Disconnect()
teleportLoopConnection = nil
end
notifyPlayer("Teleportation Stopped", "You have stopped teleporting.", 2)
end
-- Function to Create GUI Elements
local function createGUI()
-- Main ScreenGui
local ScreenGui = Instance.new("ScreenGui")
ScreenGui.Name = "TeleportGui"
ScreenGui.Parent = player:WaitForChild("PlayerGui")
ScreenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
-- Background Frame
local Background = Instance.new("Frame")
Background.Parent = ScreenGui
Background.Size = UDim2.new(0, 300, 0, 200)
Background.Position = UDim2.new(0.5, -150, 0.5, -100)
Background.BackgroundColor3 = GUI_THEME.BackgroundColor
Background.BorderSizePixel = 0
Background.AnchorPoint = Vector2.new(0.5, 0.5)
Background.ClipsDescendants = true
-- UI Corner
local UICorner = Instance.new("UICorner", Background)
UICorner.CornerRadius = UDim.new(0, 10)
-- Title Label
local Title = Instance.new("TextLabel")
Title.Parent = Background
Title.Size = UDim2.new(1, 0, 0, 40)
Title.Position = UDim2.new(0, 0, 0, 0)
Title.BackgroundTransparency = 1
Title.Text = "Teleportation Panel"
Title.Font = Enum.Font.GothamBold
Title.TextSize = 20
Title.TextColor3 = GUI_THEME.TextColor
Title.TextStrokeColor3 = Color3.fromRGB(0,0,0)
Title.TextStrokeTransparency = 0.8
Title.TextXAlignment = Enum.TextXAlignment.Center
-- Username TextBox
local TextBox = Instance.new("TextBox")
TextBox.Parent = Background
TextBox.Size = UDim2.new(0.8, 0, 0, 40)
TextBox.Position = UDim2.new(0.1, 0, 0, 60)
TextBox.PlaceholderText = "Enter Username"
TextBox.Font = Enum.Font.Gotham
TextBox.TextSize = 16
TextBox.TextColor3 = GUI_THEME.TextColor
TextBox.BackgroundColor3 = GUI_THEME.ButtonColor
TextBox.BorderSizePixel = 0
TextBox.ClearTextOnFocus = false
-- Start Button
local StartButton = Instance.new("TextButton")
StartButton.Parent = Background
StartButton.Size = UDim2.new(0.35, 0, 0, 40)
StartButton.Position = UDim2.new(0.1, 0, 0, 120)
StartButton.Text = "Start"
StartButton.Font = Enum.Font.GothamBold
StartButton.TextSize = 16
StartButton.TextColor3 = GUI_THEME.TextColor
StartButton.BackgroundColor3 = GUI_THEME.ButtonColor
StartButton.BorderSizePixel = 0
-- Stop Button
local StopButton = Instance.new("TextButton")
StopButton.Parent = Background
StopButton.Size = UDim2.new(0.35, 0, 0, 40)
StopButton.Position = UDim2.new(0.55, 0, 0, 120)
StopButton.Text = "Stop"
StopButton.Font = Enum.Font.GothamBold
StopButton.TextSize = 16
StopButton.TextColor3 = GUI_THEME.TextColor
StopButton.BackgroundColor3 = GUI_THEME.ButtonColor
StopButton.BorderSizePixel = 0
-- Hover Effects
local function addHoverEffect(button)
button.MouseEnter:Connect(function()
button.BackgroundColor3 = GUI_THEME.ButtonHoverColor
end)
button.MouseLeave:Connect(function()
button.BackgroundColor3 = GUI_THEME.ButtonColor
end)
end
addHoverEffect(StartButton)
addHoverEffect(StopButton)
-- Button Click Events
StartButton.MouseButton1Click:Connect(function()
local username = TextBox.Text
if username and username ~= "" then
startTeleportLoop(username)
else
notifyPlayer("Input Error", "Please enter a valid username.", 2)
end
end)
StopButton.MouseButton1Click:Connect(function()
stopTeleportLoop()
end)
end
-- Initial GUI Setup
createGUI()
-- Reset Teleportation on Character Respawn
player.CharacterAdded:Connect(function(char)
stopTeleportLoop()
end)
-- Optional: Toggle GUI Visibility with a Key (e.g., Right Shift)
local guiVisible = true
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.RightShift then
guiVisible = not guiVisible
local gui = player.PlayerGui:FindFirstChild("TeleportGui")
if gui then
gui.Enabled = guiVisible
end
end
end)
Explanation of Enhancements:
Aesthetic GUI Enhancements:
Background Frame: Added a central frame with rounded corners for a modern look.
Title Label: A prominent title to indicate the purpose of the GUI.
Username TextBox: Styled to match the overall theme with placeholder text.
Start and Stop Buttons: Clearly labeled buttons with hover effects for better interactivity.
Hover Effects: Buttons change color on hover to provide visual feedback.
Continuous Teleportation:
Start Button: Initiates a loop that continuously teleports the player behind the specified target at intervals defined by TELEPORT_INTERVAL.
Stop Button: Allows the player to halt the teleportation loop.
Loop Implementation: Utilized RunService.RenderStepped to handle the teleportation loop smoothly.
Precise Teleportation with CFrame:
Calculated the new CFrame based on the player’s current facing direction to ensure seamless teleportation without abrupt changes in orientation.
Maintaining Player’s Facing Direction:
The player's LookVector is preserved by calculating the new CFrame using CFrame.new(position, position + lookVector), ensuring the player maintains their original facing direction after teleportation.
Additional Features:
GUI Toggle: Implemented the ability to toggle the GUI visibility using the Right Shift key for convenience.
Input Validation: Ensured that the username field is not empty before attempting to start teleportation.
Notifications: Informative notifications guide the player through successful or failed actions.
Usage Instructions:
Opening the GUI:
The GUI appears centered on the screen upon script execution.
Alternatively, press the Right Shift key to toggle the GUI visibility.
Teleporting:
Enter the target player's username in the Enter Username text box.
Click the Start button to begin continuously teleporting behind the specified player.
Click the Stop button to halt the teleportation loop.
Notifications:
You'll receive notifications for successful teleportation starts, stops, and any errors (e.g., invalid usernames).
Notes:
Permissions: Ensure that the script is executed in a context that has the necessary permissions, such as a LocalScript within StarterPlayerScripts or StarterGui.
Target Validation: The script currently searches for players based on exact username matches. Ensure the username is correctly spelled and exists in the game.
Performance: Continuously teleporting can have performance implications, especially with lower teleport intervals. Adjust TELEPORT_INTERVAL as needed to balance responsiveness and performance.
Feel free to further customize the GUI aesthetics or teleportation logic to better fit your game's design and requirements!