Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to create a button in the player's UI
- local function createTeleportButton()
- -- Create a ScreenGui
- local screenGui = Instance.new("ScreenGui")
- screenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
- -- Create the button
- local button = Instance.new("TextButton")
- button.Size = UDim2.new(0, 40, 0, 40) -- Smaller button size
- button.Position = UDim2.new(1, -60, 0, 10) -- Position at top right corner
- button.BackgroundColor3 = Color3.fromRGB(0, 0, 128) -- Dark blue color
- button.Text = "Invisible" -- Button text
- button.TextColor3 = Color3.fromRGB(255, 255, 255) -- White text
- button.FontSize = Enum.FontSize.Size14 -- Smaller text size
- button.TextScaled = true -- Automatically scale text to fit button size
- button.Parent = screenGui
- -- Create squircle shape
- local uiCorner = Instance.new("UICorner") -- Create a UICorner instance
- uiCorner.CornerRadius = UDim.new(0, 20) -- Adjust the radius for squircle effect
- uiCorner.Parent = button
- local lastPosition = nil
- local camera = game.Workspace.CurrentCamera
- local randomTeleportDistance = 500 -- Minimum distance for random teleport
- local isFrozen = false -- Track whether the character is frozen
- local originalCameraPosition = nil -- Store original camera CFrame
- local dragging = false
- local lastTouchPosition = nil -- Store the last touch position for dragging
- local UserInputService = game:GetService("UserInputService") -- Input handling service
- -- Function to show notification
- local function showNotification()
- local player = game.Players.LocalPlayer
- player:WaitForChild("PlayerGui"):SetCore("SendNotification", {
- Title = "Teleport Button Created",
- Text = "Click the 'Invisible' button to teleport your character while keeping the camera fixed!",
- Duration = 5 -- Duration in seconds
- })
- end
- -- Function to teleport the character to a random position
- local function teleportToRandomPosition()
- local player = game.Players.LocalPlayer
- local character = player.Character
- if character and character:FindFirstChild("HumanoidRootPart") then
- local humanoidRootPart = character.HumanoidRootPart
- local newPosition
- -- Capture and lock the camera's current position
- originalCameraPosition = camera.CFrame
- -- Generate a new position until it is far enough from the current position
- repeat
- newPosition = Vector3.new(
- math.random(-2000, 2000), -- Increased random range
- 50, -- Adjust Y position to prevent falling
- math.random(-2000, 2000) -- Increased random range
- )
- until (newPosition - humanoidRootPart.Position).magnitude > randomTeleportDistance
- lastPosition = humanoidRootPart.Position
- humanoidRootPart.CFrame = CFrame.new(newPosition)
- -- Freeze the character and prevent falling
- local humanoid = character:FindFirstChild("Humanoid")
- if humanoid then
- isFrozen = true
- humanoid.PlatformStand = true -- Freeze the character in place
- humanoidRootPart.Anchored = true -- Prevent falling
- end
- -- Make the camera draggable after teleport
- camera.CameraType = Enum.CameraType.Scriptable -- Set camera type for custom control
- end
- end
- -- Function to teleport the character back to the last position
- local function teleportBack()
- local player = game.Players.LocalPlayer
- local character = player.Character
- if character and character:FindFirstChild("HumanoidRootPart") and lastPosition then
- character.HumanoidRootPart.CFrame = CFrame.new(lastPosition)
- -- Unfreeze the character and allow falling again
- local humanoid = character:FindFirstChild("Humanoid")
- if humanoid then
- isFrozen = false
- humanoid.PlatformStand = false -- Unfreeze the character
- character.HumanoidRootPart.Anchored = false -- Allow falling again
- end
- -- Return the camera to its default behavior
- camera.CameraType = Enum.CameraType.Custom -- Free camera control again
- lastPosition = nil -- Clear the last position after teleporting back
- end
- end
- local toggleTeleport = false
- -- Connect the button click event
- button.MouseButton1Click:Connect(function()
- if not toggleTeleport then
- teleportToRandomPosition() -- Teleport to a random position
- else
- teleportBack() -- Teleport back to the last position
- end
- toggleTeleport = not toggleTeleport -- Toggle between teleporting and returning
- end)
- -- Handle touch input for dragging on mobile
- UserInputService.TouchStarted:Connect(function(input)
- if toggleTeleport and input.UserInputType == Enum.UserInputType.Touch then
- dragging = true
- lastTouchPosition = input.Position -- Store initial touch position
- end
- end)
- UserInputService.TouchMoved:Connect(function(input)
- if dragging and toggleTeleport and input.UserInputType == Enum.UserInputType.Touch then
- local delta = input.Position - lastTouchPosition -- Calculate change in touch position
- local moveDirection = Vector3.new(-delta.X * 0.1, delta.Y * 0.1, 0) -- Adjust movement scale
- camera.CFrame = camera.CFrame * CFrame.new(moveDirection) -- Move the camera
- lastTouchPosition = input.Position -- Update touch position
- end
- end)
- UserInputService.TouchEnded:Connect(function(input)
- if input.UserInputType == Enum.UserInputType.Touch then
- dragging = false -- Stop dragging when touch ends
- end
- end)
- -- Maintain camera at the original location if frozen
- game:GetService("RunService").RenderStepped:Connect(function()
- if toggleTeleport then
- -- Camera is initially locked at the original position, but then draggable
- end
- end)
- -- Show notification upon script execution
- showNotification()
- end
- -- Execute the button creation function
- createTeleportButton()
Advertisement
Add Comment
Please, Sign In to add comment