Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to find the nearest NPC
- local function findNearestNPC()
- local nearestNPC = nil
- local nearestDistance = math.huge
- local Character = game.Players.LocalPlayer.Character
- if not Character or not Character.HumanoidRootPart then return end
- for _, folder in ipairs(workspace:GetChildren()) do
- if folder:IsA("Folder") then
- for _, child in ipairs(folder:GetChildren()) do
- if child:FindFirstChild("enemyFolder") then
- for _, npc in ipairs(child.enemyFolder:GetChildren()) do
- if npc:FindFirstChild("HumanoidRootPart") and npc:FindFirstChild("Humanoid") and npc.Humanoid.Health > 0 then
- local distance = (Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
- if distance < nearestDistance then
- nearestDistance = distance
- nearestNPC = npc
- end
- end
- end
- end
- end
- end
- end
- return nearestNPC
- end
- -- Function to check nearby mobs
- local function isNearMobs(range)
- local Character = game.Players.LocalPlayer.Character
- if not Character or not Character.HumanoidRootPart then return false end
- for _, folder in ipairs(workspace:GetChildren()) do
- if folder:IsA("Folder") then
- for _, child in ipairs(folder:GetChildren()) do
- if child:FindFirstChild("enemyFolder") then
- for _, npc in ipairs(child.enemyFolder:GetChildren()) do
- if npc:FindFirstChild("HumanoidRootPart") and npc:FindFirstChild("Humanoid") and npc.Humanoid.Health > 0 then
- local distance = (Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
- if distance <= range then
- return true
- end
- end
- end
- end
- end
- end
- end
- return false
- end
- -- Function to stay on top of target
- local function stayOnTopOfTarget()
- local Character = game.Players.LocalPlayer.Character
- local targetNPC = findNearestNPC()
- local offset = Vector3.new(0, 9, 0) -- Adjust this value as needed
- if not targetNPC or not targetNPC.Parent or not targetNPC:FindFirstChild("HumanoidRootPart") then
- wait(2) -- 2 second delay before finding next target
- return
- end
- if Character and Character:FindFirstChild("HumanoidRootPart") and targetNPC and targetNPC:FindFirstChild("HumanoidRootPart") then
- local npcPosition = targetNPC.HumanoidRootPart.Position
- local cframe = CFrame.new(npcPosition + offset, npcPosition)
- Character.HumanoidRootPart.CFrame = cframe
- end
- end
- -- Main loop example (you can modify this part)
- game:GetService("RunService").Heartbeat:Connect(function()
- stayOnTopOfTarget()
- wait(2) -- 2 second delay between teleports
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement