Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local LocalPlayer = Players.LocalPlayer
- local Character
- local CurrentTarget
- local teleportDelay = 2 -- Delay in seconds before teleporting to a new target
- local lastTeleportTime = 0 -- Tracks the last teleport time
- local VirtualInputManager = game:GetService("VirtualInputManager") -- Assuming this is defined elsewhere
- local function teleportToNearestNPC()
- local nearestNPC = nil
- local nearestDistance = math.huge
- Character = LocalPlayer.Character
- if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
- -- Find nearest NPC
- 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
- if nearestNPC then
- CurrentTarget = nearestNPC -- Store the current target
- lastTeleportTime = tick() -- Reset the last teleport time
- end
- end
- local function pressKey(key)
- VirtualInputManager:SendKeyEvent(true, key, false, nil)
- wait() -- Small delay between press and release
- VirtualInputManager:SendKeyEvent(false, key, false, nil)
- end
- game:GetService("RunService").Heartbeat:Connect(function()
- if CurrentTarget and CurrentTarget:FindFirstChild("HumanoidRootPart") and CurrentTarget.Humanoid.Health > 0 then
- if Character and Character:FindFirstChild("HumanoidRootPart") then
- -- Check if the character is grounded
- if Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall then return end
- -- Check if enough time has passed to teleport
- local currentTime = tick()
- if currentTime - lastTeleportTime >= teleportDelay then
- -- Ensure the character is close enough before teleporting
- local distanceToTarget = (Character.HumanoidRootPart.Position - CurrentTarget.HumanoidRootPart.Position).Magnitude
- if distanceToTarget <= 50 then -- Adjust this distance as needed
- -- Teleport to the target position directly above the NPC
- Character.HumanoidRootPart.CFrame = CFrame.new(CurrentTarget.HumanoidRootPart.Position + Vector3.new(0, 2, 0))
- lastTeleportTime = currentTime -- Update the last teleport time
- else
- -- Move closer to the target without teleporting
- Character.HumanoidRootPart.Position = Character.HumanoidRootPart.Position:Lerp(CurrentTarget.HumanoidRootPart.Position + Vector3.new(0, 2, 0), 0.1) -- Smooth movement
- end
- else
- -- Position the character above the target without teleporting
- Character.HumanoidRootPart.Position = CurrentTarget.HumanoidRootPart.Position + Vector3.new(0, 2, 0)
- end
- -- Press keys if near mobs
- if (Character.HumanoidRootPart.Position - CurrentTarget.HumanoidRootPart.Position).Magnitude <= 20 then
- pressKey("E") -- Use virtual key presses
- pressKey("Q")
- end
- end
- else
- CurrentTarget = nil -- Clear target if it's dead or gone
- teleportToNearestNPC() -- Find a new target
- end
- end)
- -- Handle character respawn
- LocalPlayer.CharacterAdded:Connect(function(newCharacter)
- Character = newCharacter
- wait(1)
- end)
- -- Initial setup
- teleportToNearestNPC()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement