Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Locks the player's mouse and point of view onto the nearest player from a different team
- -- Toggle on/off with the equals (=) key
- local UserInputService = game:GetService("UserInputService")
- local Players = game:GetService("Players")
- local player = Players.LocalPlayer
- local camera = game.Workspace.CurrentCamera
- local isEnabled = false
- -- Function to find the nearest player from a different team
- local function findNearestEnemyPlayer()
- local nearestPlayer = nil
- local minDistance = math.huge
- local playerPosition = player.Character.HumanoidRootPart.Position
- local playerTeam = player.Team
- for _, otherPlayer in pairs(Players:GetPlayers()) do
- if otherPlayer ~= player and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") and otherPlayer.Team ~= playerTeam then
- local distance = (otherPlayer.Character.HumanoidRootPart.Position - playerPosition).Magnitude
- if distance < minDistance then
- minDistance = distance
- nearestPlayer = otherPlayer
- end
- end
- end
- return nearestPlayer
- end
- -- Function to handle key press
- local function onKeyPress(input)
- if input.KeyCode == Enum.KeyCode.Equals then
- isEnabled = not isEnabled
- end
- end
- -- Connect the key press function to the UserInputService
- UserInputService.InputBegan:Connect(onKeyPress)
- -- Main loop to update camera position
- game:GetService("RunService").RenderStepped:Connect(function()
- if isEnabled then
- local nearestPlayer = findNearestEnemyPlayer()
- if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
- local targetPosition = nearestPlayer.Character.HumanoidRootPart.Position
- camera.CFrame = CFrame.new(camera.CFrame.p, targetPosition)
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement