Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turret
- local part = script.Parent
- local function closest()
- local range = 9999
- local target = nil
- for _, v in pairs(game.Workspace:GetDescendants()) do
- if game.Players:FindFirstChild(v.Name) then
- local def = game.Players:FindFirstChild(v.Name)
- if def.Character then
- local JN = def.Character:FindFirstChild("HumanoidRootPart")
- if JN then
- local dist = (part.Position - JN.Position).magnitude
- if dist < range then
- range = dist
- target = def.Character
- end
- end
- end
- end
- end
- return target
- end
- local notTarget = workspace.Target
- spawn(function()
- while game:GetService("RunService").Heartbeat:Wait() do
- if closest() and closest():FindFirstChild("HumanoidRootPart") then
- local vroot = closest():FindFirstChild("HumanoidRootPart")
- local direction = (vroot.Position - part.Position).unit * 1000
- local rayset = RaycastParams.new()
- rayset.FilterType = Enum.RaycastFilterType.Blacklist
- rayset.FilterDescendantsInstances = {notTarget}
- rayset.IgnoreWater = true
- local result = workspace:Raycast(part.Position, direction, rayset)
- if result and result.Instance:IsDescendantOf(closest()) then
- part.CFrame = CFrame.lookAt(part.Position, vroot.Position)
- end
- else
- return
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement