Advertisement
Freshbloodb

Untitled

Nov 29th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local RunService = game:GetService("RunService")
  3.  
  4. local localPlayer = Players.LocalPlayer
  5. local character
  6. local humanoid
  7. local humanoidRootPart
  8.  
  9. -- Función para verificar si el jugador tiene un ítem
  10. local function playerHasItems(player)
  11. local character = player.Character
  12. if not character then return false end
  13.  
  14. if character:FindFirstChildOfClass("Tool") then
  15. return true
  16. end
  17.  
  18. local backpack = player:FindFirstChildOfClass("Backpack")
  19. if backpack and #backpack:GetChildren() > 0 then
  20. return true
  21. end
  22.  
  23. return false
  24. end
  25.  
  26. -- Función para obtener el jugador más cercano
  27. local function getClosestPlayer()
  28. local closestPlayer = nil
  29. local shortestDistance = math.huge
  30.  
  31. for _, player in ipairs(Players:GetPlayers()) do
  32. if player ~= localPlayer then
  33. local targetCharacter = player.Character
  34. if targetCharacter then
  35. local targetHumanoidRootPart = targetCharacter:FindFirstChild("HumanoidRootPart")
  36. if targetHumanoidRootPart then
  37. local distance = (humanoidRootPart.Position - targetHumanoidRootPart.Position).Magnitude
  38. if distance < shortestDistance then
  39. shortestDistance = distance
  40. closestPlayer = player
  41. end
  42. end
  43. end
  44. end
  45. end
  46.  
  47. return closestPlayer
  48. end
  49.  
  50. -- Función para hacer que el personaje mire al humanoide más cercano
  51. local function lookAtClosestPlayer()
  52. if not _G.EnableLookatplayerclosset then return end -- Verificar si la funcionalidad está habilitada
  53.  
  54. local closestPlayer = getClosestPlayer()
  55.  
  56. if closestPlayer and playerHasItems(localPlayer) then
  57. local targetCharacter = closestPlayer.Character
  58. if targetCharacter then
  59. local targetHumanoidRootPart = targetCharacter:FindFirstChild("HumanoidRootPart")
  60. if targetHumanoidRootPart then
  61. local direction = (targetHumanoidRootPart.Position - humanoidRootPart.Position).unit
  62. humanoidRootPart.CFrame = CFrame.lookAt(humanoidRootPart.Position, humanoidRootPart.Position + direction)
  63. end
  64. end
  65. end
  66. end
  67.  
  68. -- Función para reiniciar el seguimiento de la mirada al respawn
  69. local function onCharacterAdded(characterInstance)
  70. character = characterInstance
  71. humanoid = character:WaitForChild("Humanoid")
  72. humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  73.  
  74. -- Actualizar continuamente la dirección de la mirada
  75. RunService.Heartbeat:Connect(function()
  76. if _G.EnableLookatplayerclosset and playerHasItems(localPlayer) then
  77. lookAtClosestPlayer()
  78. end
  79. end)
  80. end
  81.  
  82. -- Asegurar que el código se reinicie correctamente después de cada respawn
  83. localPlayer.CharacterAdded:Connect(onCharacterAdded)
  84.  
  85. -- Si el jugador ya tiene un personaje, conectar de inmediato
  86. if localPlayer.Character then
  87. onCharacterAdded(localPlayer.Character)
  88. end
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement