Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local player = game.Players.LocalPlayer
- local camera = workspace.CurrentCamera
- local playerGui = player:WaitForChild("PlayerGui")
- -- Default values for smoothness and prediction
- local smoothSpeed = 0.9 -- Default smoothness
- local predictionValue = 0.150 -- Default prediction
- -- Variables to manage lock-on state
- local isLockedOn = false
- local lockedPlayer = nil
- -- Create the GUI when the player's character is added
- local function createGUI()
- -- Create a screen GUI for the buttons
- local screenGui = Instance.new("ScreenGui")
- screenGui.Parent = playerGui
- -- Create a button to lock on with a custom background image
- local lockButton = Instance.new("TextButton")
- lockButton.Size = UDim2.new(0, 100, 0, 50)
- lockButton.Position = UDim2.new(1, -110, 0, 10) -- Button positioned at the top-right corner
- lockButton.Text = "Lock On"
- lockButton.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
- lockButton.BackgroundTransparency = 1 -- Make background transparent
- lockButton.Parent = screenGui
- -- Set the button's background image
- local imageLabel = Instance.new("ImageLabel")
- imageLabel.Parent = lockButton
- imageLabel.Size = UDim2.new(1, 0, 1, 0)
- imageLabel.BackgroundTransparency = 1
- imageLabel.Image = "https://pin.it/3C4XClbjH" -- Set image to the URL provided
- imageLabel.ScaleType = Enum.ScaleType.Stretch
- -- Create a new button for showing settings
- local settingsButton = Instance.new("TextButton")
- settingsButton.Size = UDim2.new(0, 100, 0, 50)
- settingsButton.Position = UDim2.new(0.5, -50, 0, 10) -- Button just below the "Lock On" button
- settingsButton.Text = "Settings"
- settingsButton.BackgroundColor3 = Color3.fromRGB(0, 0, 255)
- settingsButton.BackgroundTransparency = 1 -- Make background transparent
- settingsButton.Parent = screenGui
- -- Create a frame for the settings tab (hidden by default)
- local settingsFrame = Instance.new("Frame")
- settingsFrame.Size = UDim2.new(0, 200, 0, 60)
- settingsFrame.Position = UDim2.new(0.5, -100, 0, 70) -- Positioned below the settings button
- settingsFrame.BackgroundColor3 = Color3.fromRGB(50, 50, 50) -- Dark background
- settingsFrame.Parent = screenGui
- settingsFrame.Visible = false -- Start with the settings frame hidden
- -- Smoothness label and textbox
- local smoothnessLabel = Instance.new("TextLabel")
- smoothnessLabel.Size = UDim2.new(0, 100, 0, 30)
- smoothnessLabel.Position = UDim2.new(0, 10, 0, 10)
- smoothnessLabel.Text = "Smoothness:"
- smoothnessLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
- smoothnessLabel.Parent = settingsFrame
- local smoothnessBox = Instance.new("TextBox")
- smoothnessBox.Size = UDim2.new(0, 80, 0, 30)
- smoothnessBox.Position = UDim2.new(0, 110, 0, 10)
- smoothnessBox.Text = tostring(smoothSpeed)
- smoothnessBox.Parent = settingsFrame
- -- Prediction label and textbox
- local predictionLabel = Instance.new("TextLabel")
- predictionLabel.Size = UDim2.new(0, 100, 0, 30)
- predictionLabel.Position = UDim2.new(0, 10, 0, 40)
- predictionLabel.Text = "Prediction:"
- predictionLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
- predictionLabel.Parent = settingsFrame
- local predictionBox = Instance.new("TextBox")
- predictionBox.Size = UDim2.new(0, 80, 0, 30)
- predictionBox.Position = UDim2.new(0, 110, 0, 40)
- predictionBox.Text = tostring(predictionValue)
- predictionBox.Parent = settingsFrame
- -- Function to find the closest player to the middle of the screen
- local function getClosestPlayerToCenter()
- local closestPlayer = nil
- local shortestDistance = math.huge -- Start with a very large distance
- -- Find the center of the screen (middle point)
- local screenCenter = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
- for _, p in pairs(game.Players:GetPlayers()) do
- if p ~= player then
- local character = p.Character
- if character and character:FindFirstChild("Head") then
- local head = character.Head
- local headPos = camera:WorldToScreenPoint(head.Position)
- -- Calculate the distance from the screen center to the player's head
- local distance = (screenCenter - Vector2.new(headPos.X, headPos.Y)).Magnitude
- -- Keep track of the closest player to the center of the screen
- if distance < shortestDistance then
- shortestDistance = distance
- closestPlayer = p
- end
- end
- end
- end
- return closestPlayer
- end
- -- Function to smoothly drag the camera towards the locked player (with prediction)
- local function smoothlyDragToPlayer(targetPlayer)
- if targetPlayer and targetPlayer.Character then
- local character = targetPlayer.Character
- local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
- if humanoidRootPart then
- -- Get the player's current velocity
- local humanoid = character:FindFirstChild("Humanoid")
- local velocity = humanoidRootPart.Velocity
- local predictedPosition = humanoidRootPart.Position + velocity * predictionValue -- Use the dynamic prediction value
- -- Interpolate between the current camera position and the predicted position
- local currentPosition = camera.CFrame.Position
- local newCameraPosition = currentPosition:Lerp(predictedPosition, smoothSpeed)
- -- Smoothly update the camera position
- camera.CFrame = CFrame.new(newCameraPosition, predictedPosition)
- end
- end
- end
- -- Function to lock onto the nearest player
- local function lockOn()
- local targetPlayer = getClosestPlayerToCenter()
- if targetPlayer then
- lockedPlayer = targetPlayer
- isLockedOn = true
- lockButton.Text = "Lock On (Active)"
- cameraShake()
- -- Listen for when the locked player's humanoid dies or becomes ragdolled
- local function onHumanoidDied()
- stopLockingOn() -- Stop locking on if the player dies or ragdolls
- end
- local function onHumanoidHealthChanged()
- -- Check if the humanoid health is less than or equal to 0 (player is knocked out or ragdolled)
- if lockedPlayer and lockedPlayer.Character then
- local humanoid = lockedPlayer.Character:FindFirstChildOfClass("Humanoid")
- if humanoid and humanoid.Health <= 0 then
- stopLockingOn() -- Stop lock-on if the player is knocked out
- end
- end
- end
- -- Add events to listen for death or ragdolling
- if lockedPlayer and lockedPlayer.Character then
- local humanoid = lockedPlayer.Character:FindFirstChildOfClass("Humanoid")
- if humanoid then
- humanoid.Died:Connect(onHumanoidDied) -- Listen for death
- humanoid.HealthChanged:Connect(onHumanoidHealthChanged) -- Listen for health changes
- end
- end
- else
- print("No player found to lock onto")
- end
- end
- -- Function to stop locking on
- local function stopLockingOn()
- lockedPlayer = nil
- isLockedOn = false
- lockButton.Text = "Lock On"
- end
- -- Camera shake effect
- local function cameraShake()
- local originalPosition = camera.CFrame.Position
- for i = 1, 10 do
- camera.CFrame = CFrame.new(originalPosition + Vector3.new(math.random(), math.random(), math.random()) * 0.1)
- wait(0.05)
- end
- camera.CFrame = CFrame.new(originalPosition)
- end
- -- Connect the button click event to toggle lock-on
- lockButton.MouseButton1Click:Connect(function()
- if isLockedOn then
- stopLockingOn() -- Stop lock-on if already active
- else
- lockOn() -- Lock onto the closest player to the middle of the screen
- end
- end)
- -- Show/hide the settings tab when the settings button is clicked
- settingsButton.MouseButton1Click:Connect(function()
- settingsFrame.Visible = not settingsFrame.Visible -- Toggle visibility
- end)
- -- Update to continuously drag the camera towards the locked player's predicted position
- game:GetService("RunService").Heartbeat:Connect(function()
- if isLockedOn and lockedPlayer then
- smoothlyDragToPlayer(lockedPlayer) -- Smooth
Advertisement
Add Comment
Please, Sign In to add comment