Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- LocalScript placed in a Tool or Part
- local UserInputService = game:GetService("UserInputService")
- local Players = game:GetService("Players")
- local Workspace = game:GetService("Workspace")
- local mouse = game.Players.LocalPlayer:GetMouse()
- local isLocked = false
- local lockedPlayer = nil
- local function findClosestPlayer()
- local closestPlayer = nil
- local shortestDistance = math.huge
- local mousePosition = mouse.Hit.p
- for _, player in ipairs(game.Players:GetPlayers()) do
- local character = player.Character
- if character and character:FindFirstChild("HumanoidRootPart") then
- local distance = (character.HumanoidRootPart.Position - mousePosition).magnitude
- if distance < shortestDistance then
- shortestDistance = distance
- closestPlayer = player
- end
- end
- end
- return closestPlayer
- end
- UserInputService.InputBegan:Connect(function(input, isProcessed)
- if isProcessed then return end
- if input.KeyCode == Enum.KeyCode.E then
- isLocked = not isLocked
- if isLocked then
- lockedPlayer = findClosestPlayer()
- if lockedPlayer then
- print("Locked onto:", lockedPlayer.Name)
- end
- else
- print("Lock released")
- lockedPlayer = nil
- end
- end
- end)
- game:GetService("RunService").Heartbeat:Connect(function()
- if isLocked and lockedPlayer then
- -- Example: You might want to update the tool's position to follow lockedPlayer
- -- Replace this with your desired behavior (e.g., aiming the tool towards the lockedPlayer)
- -- For demonstration purposes, let's just print the distance.
- local distance = (lockedPlayer.Character.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
- print("Distance to locked player:", distance)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment