Hello, I am an Intermediate Programmer on Roblox and I want to present to you a complex RPG-style enemy. This waits until the player is close and then attacks with a custom animation that you made! You can also customize the damage, attack speed, and distance until it walks and attacks you.
Feel free to add anything to this Script as it is open-source and free.
The Script:
local dps = 5
local waitUntilAttack = 2
local distanceToAttack = 15
local animation = script.Parent.Attack
--Customization above
local mags = {}
local req = require(script.Parent.req)
local db = true
local db2 = true
local db3 = true
local pathService = game:GetService("PathfindingService")
local path = pathService:CreatePath()
local function pathfind(pos)
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, pos)
for i,v in pairs(path:GetWaypoints()) do
script.Parent.Humanoid:MoveTo(v.Position)
end
end
local function randomWalk()
local randomPos = Vector3.new(script.Parent.HumanoidRootPart.Position.X + math.random(-5,5),0,script.Parent.HumanoidRootPart.Position.Z + math.random(-5,5))
pathfind(randomPos)
end
local function analyzePlayerPositions()
table.clear(mags)
local plrs = req.getCharacters()
for i,v in pairs(plrs) do
local mag = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
table.insert(mags, mag)
if i == #plrs then
if mag < distanceToAttack then
pathfind(v.HumanoidRootPart.Position)
else if db3 == true then
db3 = false
randomWalk()
wait(1)
db3 = true
end
end
if mag < 5 and db == true and script.Parent.Humanoid.Health > 0 then
db = false
script.Parent.Humanoid.Animator:LoadAnimation(animation):Play()
script.Parent.Humanoid.WalkSpeed = 0
v.Humanoid.Health -= dps
wait(waitUntilAttack)
script.Parent.Humanoid.WalkSpeed = 12
db = true
end
end
end
end
script.Parent.Humanoid.HealthChanged:Connect(function(amount)
local guiClone = game.ServerStorage.GUIs.HealthGui:Clone()
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Transparency = 1
part.Parent = workspace
part.Position = script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-2,2),math.random(-2,2),math.random(-2,2))
guiClone.Parent = part
guiClone.TextLabel.Text = tostring(amount)
wait(3)
part:Destroy()
end)
while wait(.2) do
analyzePlayerPositions()
end
Module Script you will need [req Variable above]:
local module = {}
local plrs = {}
function module.addCharacter(v)
table.insert(plrs, v)
end
function module.removeCharacter(v)
for i,x in pairs(plrs) do
if x.Name == v.Name then
table.remove(plrs, i)
end
end
end
function module.getCharacters()
return plrs
end
return module