Advertisement
SigmaBoy456

Roblox Turret Raycast

Oct 3rd, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. -- Turret
  2. local part = script.Parent
  3. local function closest()
  4. local range = 9999
  5. local target = nil
  6. for _, v in pairs(game.Workspace:GetDescendants()) do
  7. if game.Players:FindFirstChild(v.Name) then
  8. local def = game.Players:FindFirstChild(v.Name)
  9. if def.Character then
  10. local JN = def.Character:FindFirstChild("HumanoidRootPart")
  11. if JN then
  12. local dist = (part.Position - JN.Position).magnitude
  13. if dist < range then
  14. range = dist
  15. target = def.Character
  16. end
  17. end
  18. end
  19. end
  20. end
  21. return target
  22. end
  23.  
  24. local notTarget = workspace.Target
  25.  
  26. spawn(function()
  27. while game:GetService("RunService").Heartbeat:Wait() do
  28. if closest() and closest():FindFirstChild("HumanoidRootPart") then
  29. local vroot = closest():FindFirstChild("HumanoidRootPart")
  30. local direction = (vroot.Position - part.Position).unit * 1000
  31. local rayset = RaycastParams.new()
  32. rayset.FilterType = Enum.RaycastFilterType.Blacklist
  33. rayset.FilterDescendantsInstances = {notTarget}
  34. rayset.IgnoreWater = true
  35. local result = workspace:Raycast(part.Position, direction, rayset)
  36. if result and result.Instance:IsDescendantOf(closest()) then
  37. part.CFrame = CFrame.lookAt(part.Position, vroot.Position)
  38. end
  39. else
  40. return
  41. end
  42. end
  43. end)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement