Hullo, I would like to make my code easier to edit and not have to make new variables every time I wanna make a new enemy for my game.
The code I wanna tidy up
local RunOnce = false
local FastZombieDB = false
local enemyHitbox
function EnemyAI.FastZombie(humrp,hum,Enemy)
if FastZombieDB == false then
FastZombieDB = true
--local AnimId = 'rbxassetid://6932293833'
local AnimId = 'rbxassetid://11188195611'
local Damage = 15
local delay = 1
if RunOnce == false then
print('Running once')
RunOnce = true
enemyHitbox = RaycastHitbox.new(Enemy.RightHand)
enemyHitbox:SetPoints(Enemy.RightHand,{Vector3.new(0,0,0),Vector3.new(-1,1,2),Vector3.new(-3.5,1,2)})
end
if not Anims:FindFirstChild(AnimId) then
local animationInstance = Instance.new('Animation')
animationInstance.Parent = Anims
animationInstance.AnimationId = AnimId
animationInstance.Name = AnimId
end
local AttackAnim = hum:LoadAnimation(Anims[AnimId])
AttackAnim:Play()
enemyHitbox:HitStart()
enemyHitbox.OnHit:Connect(function(hit,humanoid)
if humanoid.Parent.Name ~= Enemy.Name then
humanoid:TakeDamage(Damage)
end
end)
coroutine.wrap(function()
AttackAnim.Stopped:Wait()
enemyHitbox:HitStop()
task.wait(delay)
FastZombieDB = false
end)()
end
end
Entire module
local Players = game:GetService('Players')
local RunService = game:GetService('RunService')
local PathfindingService = game:GetService('PathfindingService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)
local maxDistance = math.huge
local Anims = script.Parent.Anims
local EnemyAI = {}
EnemyAI.__index = EnemyAI
function EnemyAI.new(Enemy,WalkAnim)
local EnemyProperties = setmetatable({},EnemyAI)
local hum = Enemy.Humanoid
local humrp = Enemy.HumanoidRootPart
local WalkAnim = hum:LoadAnimation(WalkAnim)
EnemyProperties.Model = Enemy
EnemyProperties.Humanoid = hum
EnemyProperties.humrp = humrp
--Pathfinding
Enemy.PrimaryPart:SetNetworkOwner(nil)
local CoreOfFactory = workspace.CentreOfFactory
local function findTarget()
local allies = workspace.Allies:GetChildren()
local MaxDistance = 200
local nearestTarget
for _,ally in pairs(allies) do
if ally then
if ally.Humanoid.Health > 0 then
local target = ally
local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude
if distance < MaxDistance then
nearestTarget = target
MaxDistance = distance
end
end
end
end
return nearestTarget
end
local function agro()
repeat task.wait(0.05)
local target = findTarget()
if target and target.HumanoidRootPart then
local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude
--[[ local path = PathfindingService:CreatePath()
local success, errorMessage = pcall(function()
path:ComputeAsync(humrp.Position, target.HumanoidRootPart.Position + target.HumanoidRootPart.Velocity.Unit * 3)
end)]]
-- if success and path.Status == Enum.PathStatus.Success then
-- local waypoints = path:GetWaypoints()
-- for _,waypoint in pairs(waypoints) do
-- if waypoint.Action == Enum.PathWaypointAction.Jump then
-- hum:ChangeState(Enum.HumanoidStateType.Jumping)
-- end
if distance <= 5 then
EnemyAI[string.gsub(Enemy.Name,' ','')](humrp,hum,Enemy)
else
local targetVel = target.PrimaryPart.Velocity
local leadBy = 7
local lead
if targetVel.Magnitude < 0.1 then
lead = Vector3.new(0, 0, 0)
else
lead = targetVel.Unit * leadBy
end
hum:MoveTo(target.PrimaryPart.Position + lead)
end
-- end
-- end
end
until target == nil or target.Humanoid.Health == 0 or (humrp.Position - target.HumanoidRootPart.Position).Magnitude > maxDistance
end
local function walkToCore()
local path = PathfindingService:CreatePath()
local success, errorMessage = pcall(function()
path:ComputeAsync(humrp.Position, CoreOfFactory.Position)
end)
WalkAnim:Play()
if success and path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _,waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
if findTarget() and (humrp.Position - findTarget().HumanoidRootPart.Position).Magnitude < maxDistance then
agro()
end
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
WalkAnim:Stop()
end
walkToCore()
end
local RunOnce = false
local FastZombieDB = false
local enemyHitbox
function EnemyAI.FastZombie(humrp,hum,Enemy)
if FastZombieDB == false then
FastZombieDB = true
--local AnimId = 'rbxassetid://6932293833'
local AnimId = 'rbxassetid://11188195611'
local Damage = 15
local delay = 1
if RunOnce == false then
print('Running once')
RunOnce = true
enemyHitbox = RaycastHitbox.new(Enemy.RightHand)
enemyHitbox:SetPoints(Enemy.RightHand,{Vector3.new(0,0,0),Vector3.new(-1,1,2),Vector3.new(-3.5,1,2)})
end
if not Anims:FindFirstChild(AnimId) then
local animationInstance = Instance.new('Animation')
animationInstance.Parent = Anims
animationInstance.AnimationId = AnimId
animationInstance.Name = AnimId
end
local AttackAnim = hum:LoadAnimation(Anims[AnimId])
AttackAnim:Play()
enemyHitbox:HitStart()
enemyHitbox.OnHit:Connect(function(hit,humanoid)
if humanoid.Parent.Name ~= Enemy.Name then
humanoid:TakeDamage(Damage)
end
end)
coroutine.wrap(function()
AttackAnim.Stopped:Wait()
enemyHitbox:HitStop()
task.wait(delay)
FastZombieDB = false
end)()
end
end
return EnemyAI
-
The code makes a new enemy which does specific things when it attacks based on the ‘The code I wanna tidy up’ function.
When I call this, it finds the entity ‘fast zombie’ and then I just input the animations, some values such as the damage and boom i’ve got a new enemy, i’m planning on putting the enemies in replicatedstorage later for cloning purposes. -
I’ve considered making it oop but I couldn’t figure out how to do the : parameter when calling it, i’ve looked at this post and several others yet I couldn’t make them work.
-
I just want to make it easier to copy and paste so that I can make a lot of enemy attacks very fast
If anyone needs a place download, then I will happily provide.