So, to start, there isnt any errors, warnings, etc. It works almost perfectly as I intended, except for 1 issue where after killing 1 enemy, the swordsman just completely stopped attacking or running unless it drops under the fearless minimum.
It just stands completely still, and I do not know why, and I’m not in the mood to scour 200 lines of code for a few hours to change 1 line or something as it is quite late, help?
(if you see anything that can be optimized do let me know)
stuff = {
findrange = 150, -- the range for which the enemy sees players
atkrange = 8, -- attacking range
damage = 15, -- damage for each attack
sightrange = 0.74, --its of 1 (0.5 would be 50% of vision or 180 degree vision)
ws = 20, --walkspeed
findcd = 0.5, --how fast the finding loop is
waypointamount = 7, --amount of waypoints = more accurate path at the cost of performance
fearless = false, --if fearless is enabled, if its disabled the enemy runs away at a certain health
fearlessmin = 3 --divisor, example: 3 = 1/3 of max health
}
local atkcd = false
--some extra variables
anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Humanoid.Animation)
mod = require(game.ServerScriptService.Modules.MoveToFix)
--finding function
local function find()
local en
for i, v in pairs(game.Players:GetPlayers()) do
if (v.Character.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude < stuff.findrange and not v.Character.HumanoidRootPart:FindFirstChild("Good") and not v.Character.HumanoidRootPart:FindFirstChild("Enemy") then
en = v.Character.HumanoidRootPart
return en
else
return nil
end
end
local en
for i, v in pairs(workspace.BOTS.Enemies.Pirates:GetChildren()) do
if (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude < stuff.findrange then
en = v.HumanoidRootPart
return en
else
return nil
end
end
local en
for i, v in pairs(workspace.BOTS.Enemies.WaterBois:GetChildren()) do
if (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude < stuff.findrange then
en = v.HumanoidRootPart
return en
else
return nil
end
end
local en
for i, v in pairs(workspace.BOTS.Enemies.Goblins:GetChildren()) do
if (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude < stuff.findrange then
en = v.HumanoidRootPart
return en
else
return nil
end
end
local en
for i, v in pairs(workspace.BOTS.Bad:GetChildren()) do
if (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude < stuff.findrange then
en = v.HumanoidRootPart
return en
else
return nil
end
end
end
--attack function
local function atk(v)
local en
if (v.Position - script.Parent.HumanoidRootPart.Position).Magnitude < stuff.atkrange then
if v == nil then return end
if v:FindFirstChild("Good") then
if v:FindFirstChild("Enemy") then
en = v
return en
end
else
en = v
return en
end
end
end
local function pathfind(enemy)
if script.Parent.Humanoid.Health > script.Parent.Humanoid.MaxHealth/stuff.fearlessmin or enemy.Parent.Humanoid.Health < script.Parent.Humanoid.Health then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, enemy.Position)
for i, v in pairs(script.Parent.HumanoidRootPart.Attachment:GetChildren()) do
v.Enabled = false
end
local waypoints = path:GetWaypoints()
for i = 1, stuff.waypointamount do
if i <= #waypoints then
if enemy ~= nil then
if waypoints[i].Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
script.Parent.Humanoid:MoveTo(waypoints[i].Position)
script.Parent.Humanoid.MoveToFinished:Wait()
pcall(
mod.setNetworkOwnerOfModel, script.Parent
)
else
return
end
end
end
end
end
local function runaway(enemy)
if script.Parent.Humanoid.Health < script.Parent.Humanoid.MaxHealth/stuff.fearlessmin and enemy.Parent.Humanoid.Health > script.Parent.Humanoid.Health then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, enemy.CFrame.LookVector*(enemy.Position - script.Parent.HumanoidRootPart.Position).Magnitude)
for i, v in pairs(script.Parent.HumanoidRootPart.Attachment:GetChildren()) do
v.Enabled = true
end
local waypoints = path:GetWaypoints()
for i = 1, stuff.waypointamount do
if i <= #waypoints then
if enemy ~= nil then
if waypoints[i].Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
script.Parent.Humanoid:MoveTo(waypoints[i].Position)
script.Parent.Humanoid.MoveToFinished:Wait()
pcall(
mod.setNetworkOwnerOfModel, script.Parent
)
else
return
end
end
end
end
end
--while loop
while wait(stuff.findcd) do
local enemy = find()
if enemy ~= nil then
--while loop for pathfinding
while enemy ~= nil and enemy.Parent ~= nil and enemy.Parent.Humanoid.Health > 0 do
if enemy ~= nil and enemy.Parent ~= nil and enemy.Parent.Humanoid.Health > 0 then
task.wait(0.025)
local atk = atk(enemy)
--attack
if atk and enemy ~= nil then
print("atk")
if atkcd == false and enemy then
atkcd = true
script.Parent.HumanoidRootPart.slash:Play()
anim:Play()
if enemy.Parent.HumanoidRootPart.Defense.Value > stuff.damage-1 then
enemy.Parent.Humanoid:TakeDamage(1)
else
enemy.Parent.Humanoid:TakeDamage((stuff.damage-enemy.Parent.HumanoidRootPart.Defense.Value)/enemy.Parent.HumanoidRootPart.DamageReduction.Value)
end
wait(0.75)
atkcd = false
end
end
--safeguarding
if enemy and enemy ~= nil then
if stuff.fearless == true then
-- if its fearless then
if enemy ~= nil then
pathfind(enemy)
else
enemy = nil
atk = nil
script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
end
elseif stuff.fearless == false then
--deciding to not run today
if enemy ~= nil then
pathfind(enemy)
else
enemy = nil
atk = nil
script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
end
--running
if enemy ~= nil then
runaway(enemy)
else
enemy = nil
atk = nil
script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
end
end
end
else
enemy = nil
return
end
end
else
enemy = nil
end
end