RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"the npc made a bee line for the door instead of going to the next waypoint"
-- server script local isAttacking = false local npc = game.Workspace.NPC local door = game.Workspace.Door -- assuming there is a door in your Workspace local waypoints = script.Parent.Parent.Parent.Waypoints local waypointIndex = 1 -- Function to attack a player local function attackPlayer(player) if isAttacking then return end -- if the NPC is already attacking, don't run the function again isAttacking = true npc.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position) while (player.Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude > 5 do wait(0.1) -- wait for a short time before checking the distance again end local attackAnimation = npc.Humanoid:LoadAnimation(npc.Animations.AttackAnimation) attackAnimation:Play() wait(2) -- attack cooldown attackAnimation:Stop() if (player.Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude < 5 then player.Character.Humanoid:TakeDamage(10) print("NPC damaged the player!") -- log the damage event end isAttacking = false end -- Function to make the NPC move to the door directly local function moveToDoor() while true do if not isAttacking then local nearestPlayer = nil local nearestPlayerDistance = 30 -- only consider players within 30 units for _, player in pairs(game:GetService("Players"):GetPlayers()) do if player.Character then local distance = (player.Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude if distance < nearestPlayerDistance then nearestPlayer = player nearestPlayerDistance = distance end end end if nearestPlayer then attackPlayer(nearestPlayer) else npc.Humanoid:MoveTo(door.Position) end end wait(0.1) -- wait for a short time before checking the distance again end end -- Start the function moveToDoor()