Advertisement
McSkibidi

SWORDBOT - Roblox AI bot script

Feb 8th, 2025 (edited)
277
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.62 KB | None | 0 0
  1. -- Services
  2. local Players = game:GetService("Players")
  3. local PathfindingService = game:GetService("PathfindingService")
  4. local RunService = game:GetService("RunService")
  5. local TweenService = game:GetService("TweenService")
  6. local UserInputService = game:GetService("UserInputService")
  7.  
  8. -- Player and Character
  9. local player = Players.LocalPlayer
  10. local character = player.Character or player.CharacterAdded:Wait()
  11. local humanoid = character:WaitForChild("Humanoid")
  12. local rootPart = character:WaitForChild("HumanoidRootPart")
  13.  
  14. -- Tool (Classic Sword)
  15. local tool = character:FindFirstChildOfClass("Tool")
  16. local handle = tool and tool:FindFirstChild("Handle")
  17.  
  18. -- Pathfinding Variables
  19. local path = PathfindingService:CreatePath({
  20. AgentRadius = 2,
  21. AgentHeight = 5,
  22. AgentCanJump = true
  23. })
  24. local waypoints = {}
  25. local currentWaypointIndex = 1
  26. local isComputingPath = false
  27.  
  28. -- Combat Variables
  29. local attackRadius = 15 -- Radius within which the bot will attack
  30. local targetLockRange = 1000 -- Large radius to find and move toward players
  31. local reactionTime = 0 -- Instant reaction time
  32. local lastAttackTime = 0
  33. local attackCooldown = 1.25 -- Attack cooldown
  34. local comboCooldown = 1.5 -- Cooldown between combos
  35. local lastComboTime = 0
  36.  
  37. -- Void Detection Variables
  38. local voidCheckRayLength = 50 -- Ray length to check for void below
  39. local voidThreshold = 0.75 -- 75% of the character must be off the part to trigger void detection
  40. local autoVoidEnabled = true -- Enable/disable auto-void
  41.  
  42. -- Parkour Variables
  43. local parkourCooldown = 0.5 -- Faster parkour cooldown
  44. local lastParkourTime = 0
  45.  
  46. -- Smooth Movement Variables
  47. local smoothMoveSpeed = 30 -- Faster movement speed
  48. local smoothTurnSpeed = 0.05 -- Faster turning speed
  49.  
  50. -- Target Variables
  51. local target = nil
  52.  
  53. -- Jumping Variables
  54. local jumpCooldown = 1 -- Cooldown between jumps
  55. local lastJumpTime = 0
  56.  
  57. -- Player-Like Movement Variables
  58. local randomMovementDelay = 0.5 -- Delay between random movements
  59. local lastRandomMovementTime = 0
  60.  
  61. -- Combat State Variables
  62. local isAttacking = false
  63. local isFeinting = false
  64. local isDefensive = false
  65.  
  66. -- AI Toggle
  67. local aiEnabled = true
  68.  
  69. -- Health Awareness Variables
  70. local lowHealthThreshold = 0.25 -- 25% health
  71. local isLowHealth = false
  72.  
  73. -- Pattern Recognition Variables
  74. local playerBehavior = {} -- Stores behavior patterns for each player
  75.  
  76. -- Function to create the UI
  77. local function createUI()
  78. local screenGui = Instance.new("ScreenGui")
  79. screenGui.Name = "SwordBotUI"
  80. screenGui.Parent = player.PlayerGui
  81.  
  82. -- Main Frame (Draggable Box)
  83. local frame = Instance.new("Frame")
  84. frame.Size = UDim2.new(0, 200, 0, 80) -- 2x the size of the button
  85. frame.Position = UDim2.new(0.5, -100, 0.5, -40) -- Center of the screen
  86. frame.BackgroundColor3 = Color3.new(0.2, 0.2, 0.2)
  87. frame.BorderSizePixel = 0
  88. frame.Active = true
  89. frame.Draggable = true
  90. frame.Parent = screenGui
  91.  
  92. -- Label (SWORDBOT)
  93. local label = Instance.new("TextLabel")
  94. label.Size = UDim2.new(1, 0, 0.5, 0)
  95. label.Position = UDim2.new(0, 0, 0, 0)
  96. label.Text = "SWORDBOT"
  97. label.TextColor3 = Color3.new(1, 1, 1)
  98. label.BackgroundTransparency = 1
  99. label.Font = Enum.Font.SourceSansBold
  100. label.TextSize = 18
  101. label.Parent = frame
  102.  
  103. -- Toggle Button
  104. local toggleButton = Instance.new("TextButton")
  105. toggleButton.Size = UDim2.new(0.8, 0, 0.4, 0)
  106. toggleButton.Position = UDim2.new(0.1, 0, 0.55, 0)
  107. toggleButton.Text = "AI: ON"
  108. toggleButton.TextColor3 = Color3.new(1, 1, 1)
  109. toggleButton.BackgroundColor3 = Color3.new(0.3, 0.3, 0.3)
  110. toggleButton.BorderSizePixel = 0
  111. toggleButton.Parent = frame
  112.  
  113. toggleButton.MouseButton1Click:Connect(function()
  114. aiEnabled = not aiEnabled
  115. toggleButton.Text = aiEnabled and "AI: ON" or "AI: OFF"
  116. end)
  117.  
  118. return screenGui
  119. end
  120.  
  121. -- Function to equip the first tool in the player's inventory
  122. local function equipFirstTool()
  123. local backpack = player:FindFirstChild("Backpack")
  124. if backpack then
  125. local firstTool = backpack:FindFirstChildOfClass("Tool")
  126. if firstTool then
  127. firstTool.Parent = character
  128. tool = firstTool
  129. handle = tool:FindFirstChild("Handle")
  130. end
  131. end
  132. end
  133.  
  134. -- Create the UI
  135. local swordBotUI = createUI()
  136.  
  137. -- Ensure UI persists after death
  138. player.CharacterAdded:Connect(function()
  139. character = player.Character
  140. humanoid = character:WaitForChild("Humanoid")
  141. rootPart = character:WaitForChild("HumanoidRootPart")
  142. tool = character:FindFirstChildOfClass("Tool")
  143. handle = tool and tool:FindFirstChild("Handle")
  144.  
  145. -- Re-equip the first tool if not already equipped
  146. if not tool then
  147. equipFirstTool()
  148. end
  149.  
  150. -- Re-parent the UI to the new PlayerGui
  151. if swordBotUI then
  152. swordBotUI.Parent = player.PlayerGui
  153. end
  154. end)
  155.  
  156. -- Equip the first tool if not already equipped
  157. if not tool then
  158. equipFirstTool()
  159. end
  160.  
  161. -- Function to find the nearest opponent within a radius
  162. local function findNearestOpponent(radius)
  163. local closest = nil
  164. local closestDistance = radius or targetLockRange
  165.  
  166. for _, otherPlayer in pairs(Players:GetPlayers()) do
  167. if otherPlayer ~= player and otherPlayer.Character then
  168. local opponentRoot = otherPlayer.Character:FindFirstChild("HumanoidRootPart")
  169. if opponentRoot and otherPlayer.Character:FindFirstChild("Humanoid") and otherPlayer.Character.Humanoid.Health > 0 then
  170. local distance = (opponentRoot.Position - rootPart.Position).Magnitude
  171. if distance < closestDistance then
  172. closest = otherPlayer.Character
  173. closestDistance = distance
  174. end
  175. end
  176. end
  177. end
  178.  
  179. return closest
  180. end
  181.  
  182. -- Function to compute and follow a path
  183. local function followPath(targetPosition)
  184. if isComputingPath then return end
  185. isComputingPath = true
  186.  
  187. path:ComputeAsync(rootPart.Position, targetPosition)
  188.  
  189. if path.Status == Enum.PathStatus.Success then
  190. waypoints = path:GetWaypoints()
  191. currentWaypointIndex = 1
  192. else
  193. waypoints = {}
  194. end
  195.  
  196. isComputingPath = false
  197. end
  198.  
  199. -- Function to move smoothly to the next waypoint
  200. local function moveToNextWaypoint()
  201. if currentWaypointIndex <= #waypoints then
  202. local waypoint = waypoints[currentWaypointIndex]
  203. humanoid:MoveTo(waypoint.Position)
  204.  
  205. if waypoint.Action == Enum.PathWaypointAction.Jump then
  206. humanoid.Jump = true
  207. end
  208.  
  209. -- Smoothly turn toward the waypoint (only horizontally to avoid looking up/down)
  210. local direction = (Vector3.new(waypoint.Position.X, rootPart.Position.Y, waypoint.Position.Z) - rootPart.Position).Unit
  211. local goalCFrame = CFrame.new(rootPart.Position, rootPart.Position + direction)
  212. local tweenInfo = TweenInfo.new(smoothTurnSpeed, Enum.EasingStyle.Linear)
  213. local tween = TweenService:Create(rootPart, tweenInfo, {CFrame = goalCFrame})
  214. tween:Play()
  215.  
  216. if (rootPart.Position - waypoint.Position).Magnitude < 4 then
  217. currentWaypointIndex += 1
  218. end
  219. end
  220. end
  221.  
  222. -- Function to check if the character is near a void
  223. local function isNearVoid()
  224. local rayOrigin = rootPart.Position
  225. local rayDirection = Vector3.new(0, -voidCheckRayLength, 0)
  226. local raycastParams = RaycastParams.new()
  227. raycastParams.FilterDescendantsInstances = {character}
  228. raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
  229.  
  230. local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
  231. if not raycastResult then
  232. return true -- No ground detected, assume void
  233. end
  234.  
  235. -- Check if 75% of the character is off the part
  236. local characterSize = character:GetExtentsSize()
  237. local characterPosition = rootPart.Position
  238. local partPosition = raycastResult.Position
  239. local offPartPercentage = (characterPosition.Y - partPosition.Y) / characterSize.Y
  240.  
  241. return offPartPercentage > voidThreshold
  242. end
  243.  
  244. -- Function to check if the bot can safely navigate off an object
  245. local function canNavigateOffObject()
  246. local rayOrigin = rootPart.Position
  247. local rayDirection = rootPart.CFrame.LookVector * 10 -- Check 10 studs ahead
  248. local raycastParams = RaycastParams.new()
  249. raycastParams.FilterDescendantsInstances = {character}
  250. raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
  251.  
  252. local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
  253. if raycastResult then
  254. -- Check if the surface below is safe (not a void)
  255. local groundRayOrigin = raycastResult.Position
  256. local groundRayDirection = Vector3.new(0, -voidCheckRayLength, 0)
  257. local groundRaycastResult = workspace:Raycast(groundRayOrigin, groundRayDirection, raycastParams)
  258. if groundRaycastResult then
  259. -- Check if the part is thin (e.g., a platform that could lead to falling)
  260. local partSize = raycastResult.Instance.Size
  261. if partSize.X < 5 or partSize.Z < 5 then -- Thin parts (e.g., narrow platforms)
  262. return false -- Unsafe to navigate off the object
  263. end
  264. return true -- Safe to navigate off the object
  265. end
  266. end
  267.  
  268. return false -- Cannot safely navigate off the object
  269. end
  270.  
  271. -- Function to perform a basic attack
  272. local function basicAttack()
  273. if tool and handle and tick() - lastAttackTime > attackCooldown then
  274. tool:Activate() -- Simulate left-click (M1)
  275. lastAttackTime = tick()
  276. end
  277. end
  278.  
  279. -- Function to perform a combo attack
  280. local function performCombo()
  281. if tick() - lastComboTime < comboCooldown then
  282. return
  283. end
  284.  
  285. -- Example combo: Slash, delay, slash, jump
  286. basicAttack()
  287. wait(0.2)
  288. basicAttack()
  289. humanoid.Jump = true
  290. lastComboTime = tick()
  291. end
  292.  
  293. -- Function to feint an attack
  294. local function feintAttack()
  295. if isFeinting then return end
  296. isFeinting = true
  297.  
  298. -- Simulate starting an attack but cancel it
  299. tool:Activate()
  300. wait(0.1)
  301. tool:Deactivate()
  302.  
  303. isFeinting = false
  304. end
  305.  
  306. -- Function to lock onto the target and face them (only horizontally)
  307. local function lockOnTarget()
  308. if target and target:FindFirstChild("HumanoidRootPart") then
  309. local targetPosition = Vector3.new(target.HumanoidRootPart.Position.X, rootPart.Position.Y, target.HumanoidRootPart.Position.Z)
  310. local direction = (targetPosition - rootPart.Position).Unit
  311. local goalCFrame = CFrame.new(rootPart.Position, rootPart.Position + direction)
  312. local tweenInfo = TweenInfo.new(smoothTurnSpeed, Enum.EasingStyle.Linear)
  313. local tween = TweenService:Create(rootPart, tweenInfo, {CFrame = goalCFrame})
  314. tween:Play()
  315. end
  316. end
  317.  
  318. -- Function to handle combat logic
  319. local function handleCombat()
  320. if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
  321. -- Lock onto the target (only horizontally)
  322. lockOnTarget()
  323.  
  324. -- Attack if in range
  325. local distance = (target.HumanoidRootPart.Position - rootPart.Position).Magnitude
  326. if distance < attackRadius then
  327. -- Randomly choose between basic attack, combo, or feint
  328. local attackChoice = math.random(1, 3)
  329. if attackChoice == 1 then
  330. basicAttack()
  331. elseif attackChoice == 2 then
  332. performCombo()
  333. else
  334. feintAttack()
  335. end
  336. end
  337.  
  338. -- Juking: Randomly strafe to make combat more dynamic
  339. if math.random() < 0.3 then -- 30% chance to juke
  340. humanoid:MoveTo(rootPart.Position + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5)))
  341. end
  342. end
  343. end
  344.  
  345. -- Function to jump to evade attacks or climb objects
  346. local function jumpToEvadeOrClimb()
  347. if tick() - lastJumpTime < jumpCooldown then
  348. return
  349. end
  350.  
  351. -- Jump to evade attacks
  352. if target and target:FindFirstChild("HumanoidRootPart") then
  353. local distance = (target.HumanoidRootPart.Position - rootPart.Position).Magnitude
  354. if distance < attackRadius then
  355. humanoid.Jump = true
  356. lastJumpTime = tick()
  357. end
  358. end
  359.  
  360. -- Jump to climb objects
  361. local rayOrigin = rootPart.Position
  362. local rayDirection = rootPart.CFrame.LookVector * 5
  363. local raycastParams = RaycastParams.new()
  364. raycastParams.FilterDescendantsInstances = {character}
  365. raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
  366.  
  367. local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
  368. if raycastResult then
  369. humanoid.Jump = true
  370. lastJumpTime = tick()
  371. end
  372. end
  373.  
  374. -- Function to simulate player-like movement
  375. local function simulatePlayerMovement()
  376. if tick() - lastRandomMovementTime > randomMovementDelay then
  377. -- Randomly strafe or jump to simulate player-like behavior
  378. if math.random() < 0.5 then
  379. humanoid:MoveTo(rootPart.Position + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5)))
  380. else
  381. humanoid.Jump = true
  382. end
  383. lastRandomMovementTime = tick()
  384. end
  385. end
  386.  
  387. -- Main loop
  388. RunService.Heartbeat:Connect(function()
  389. if not aiEnabled then return end -- Stop AI if toggled off
  390.  
  391. -- Find the nearest opponent within targetLockRange
  392. target = findNearestOpponent(targetLockRange)
  393.  
  394. if target then
  395. -- Compute and follow a path to the target
  396. followPath(target.HumanoidRootPart.Position)
  397.  
  398. -- Move to the next waypoint
  399. moveToNextWaypoint()
  400.  
  401. -- Handle combat if within attackRadius
  402. handleCombat()
  403.  
  404. -- Jump to evade attacks or climb objects
  405. jumpToEvadeOrClimb()
  406.  
  407. -- Simulate player-like movement
  408. simulatePlayerMovement()
  409.  
  410. -- Check if near void
  411. if isNearVoid() and autoVoidEnabled then
  412. -- Check if the bot can safely navigate off the object
  413. if canNavigateOffObject() then
  414. autoVoidEnabled = false -- Disable auto-void temporarily
  415. else
  416. -- Avoid falling into the void
  417. humanoid.Jump = true
  418. followPath(rootPart.Position - rootPart.CFrame.LookVector * 5) -- Move back
  419. end
  420. else
  421. autoVoidEnabled = true -- Re-enable auto-void
  422. end
  423. else
  424. -- Wander or return to a default position if no target is found
  425. followPath(Vector3.new(0, 0, 0)) -- Adjust default position as needed
  426. end
  427. end)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement